本文分为以下几个部分:
基础知识
五大基本数据类型
[root@qundd src]# redis-cli 127.0.0.1:6379> config get databases #获取数据库数量 1) "databases" 2) "16" 127.0.0.1:6379> select 1 #切换到1号数据库 OK 127.0.0.1:6379[1]> select 0 #切换到0号数据库 OK 127.0.0.1:6379> dbsize #数据库大小 (integer) 0
tab 键可自动补全
127.0.0.1:6379> set name 1 #设置值,set key value OK 127.0.0.1:6379> get name #获取值 "1" 127.0.0.1:6379> dbsize #数据库大小 (integer) 1 127.0.0.1:6379> keys * #获取当前数据库中所有(*)key,key大小写敏感 1) "name" 127.0.0.1:6379> EXISTS name #是否存在name这个key,命令大小写不敏感 (integer) 1 127.0.0.1:6379> move name 1 #迁移到1号数据库 (integer) 1 127.0.0.1:6379> flushdb #清空当前数据库 OK 127.0.0.1:6379> flushall #清空所有数据库 OK 127.0.0.1:6379> clear #跟linux的clear一样,好看用的 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> dbsize (integer) 0 127.0.0.1:6379> set name 1 OK 127.0.0.1:6379> EXPIRE name 20 #设置name过期时间 20s (integer) 1 127.0.0.1:6379> ttl name #查看name剩余时间 (integer) 17 127.0.0.1:6379> ttl name #当 -2 的时候,表示已过期,-1表示永不过期 (integer) -2 127.0.0.1:6379> keys * (empty array)
127.0.0.1:6379> set k1 hello OK 127.0.0.1:6379> get k1 "hello" 127.0.0.1:6379> append k1 world (integer) 10 127.0.0.1:6379> get k1 "helloworld"
127.0.0.1:6379> strlen k1 (integer) 10
127.0.0.1:6379> del k1 (integer) 1 127.0.0.1:6379> get k1 不存在返回(nil) (nil) 127.0.0.1:6379> keys * (empty array)
127.0.0.1:6379> set k1 1 OK 127.0.0.1:6379> get k1 "1" 127.0.0.1:6379> incr k1 (integer) 2 127.0.0.1:6379> get k1 "2" 127.0.0.1:6379> decr k1 (integer) 1
incrby key value 给key增长value
decrby key value 给key减少value
127.0.0.1:6379> incrby k1 9 (integer) 10 127.0.0.1:6379> get k1 "10" 127.0.0.1:6379> DECRBY k1 9 (integer) 1
127.0.0.1:6379> set k2 "hello world" OK 127.0.0.1:6379> GETRANGE k2 0 2 "hel" 127.0.0.1:6379> GETRANGE k2 0 -1 #第二个位置使用 -1,获取所有 "hello world"
#setrange key offset value 127.0.0.1:6379> SETRANGE k2 1 aa #从第一个位置开始使用aa替换 (integer) 11 127.0.0.1:6379> get k2 "haalo world"
#setex key second value 127.0.0.1:6379> setex k3 20 v3 OK 127.0.0.1:6379> ttl k3 (integer) 18 127.0.0.1:6379> ttl k3 (integer) -2
127.0.0.1:6379> setnx k4 v4 (integer) 1 127.0.0.1:6379> setnx k4 v4 #存在则设置失败 (integer) 0
msetnx,要么同时set成功,要么均不set成功
127.0.0.1:6379> flushdb OK 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 OK 127.0.0.1:6379> mget k1 k2 k3 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> msetnx k3 v3 k4 v4 #存在k3,设置失败,k4也设置失败 (integer) 0 127.0.0.1:6379> keys * 1) "k3" 2) "k2" 3) "k1"
127.0.0.1:6379> getset k1 kk "v1" 127.0.0.1:6379> get k1 "kk"
可以做 stack,也可以做quque,
lrange截取,和String range用法相同
127.0.0.1:6379> flushall OK 127.0.0.1:6379> lpush list v1 #从左边插入 (integer) 1 127.0.0.1:6379> lpush list v2 (integer) 2 127.0.0.1:6379> lpush list v3 (integer) 3 127.0.0.1:6379> rpush list v0 #从右侧插入 (integer) 4 127.0.0.1:6379> lrange list 0 -1 #查看list中所有数据,-1和getrange一样,其他用法一样 1) "v3" 2) "v2" 3) "v1" 4) "v0"
127.0.0.1:6379> lpop list #左侧弹出,左侧添加的最后一个为v3 "v3" 127.0.0.1:6379> rpop list #右侧弹出,右侧最后一个为v0 "v0" 127.0.0.1:6379> rpop list #右侧弹出,右侧最后一个为v1 "v1" 127.0.0.1:6379> lrange list 0 -1 1) "v2"
#... 恢复之前的数据 #如果是lpush加入的,后加入的index小,如果是rpush加入的,后加入的index大 127.0.0.1:6379> lrange list 0 -1 1) "v3" 2) "v2" 3) "v1" 4) "v0" 127.0.0.1:6379> lindex list 2 #获取第二个位置的value值 "v1"
127.0.0.1:6379> llen list (integer) 4
#lrem key 个数 value值 #删除key中多少个某个元素,后加入(lpush)的先删除 127.0.0.1:6379> lrem list 1 v0 (integer) 1 127.0.0.1:6379> lrange list 0 -1 1) "v3" 2) "v2" 3) "v1"
127.0.0.1:6379> lrange list 0 -1 1) "v3" 2) "v2" 3) "v1" 127.0.0.1:6379> LTRIM list 0 1 OK 127.0.0.1:6379> lrange list 0 -1 1) "v3" 2) "v2"
#lset key 位置 value值 127.0.0.1:6379> lrange list 0 -1 1) "v3" 2) "v2" 127.0.0.1:6379> lset list 0 v1 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "v1" 2) "v2" 127.0.0.1:6379> lset l 0 1 #不存在该list报错 (error) ERR no such key
#linsert key before|after 元素 要插入的元素value值 127.0.0.1:6379> LRANGE list 0 -1 1) "v1" 2) "v2" 127.0.0.1:6379> linsert list before v1 v0 #在v1前(理解为lpush)加入值 (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "v0" 2) "v1" 3) "v2" 127.0.0.1:6379> linsert list after v2 v3 #在v2后(理解为rpush)加入值 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v0" 2) "v1" 3) "v2" 4) "v3"
将一个list rpop,再 lpush 进另一个list
127.0.0.1:6379> RPOPLPUSH list list2 "v3" 127.0.0.1:6379> lrange list 0 -1 1) "v0" 2) "v1" 3) "v2" 127.0.0.1:6379> lrange list2 0 -1 1) "v3"
127.0.0.1:6379> flushdb OK 127.0.0.1:6379> sadd set v1 (integer) 1 127.0.0.1:6379> sadd set v2 (integer) 1 127.0.0.1:6379> sadd set v3 (integer) 1
127.0.0.1:6379> SMEMBERS set 1) "v1" 2) "v2" 3) "v3"
#sismember 集合 元素 127.0.0.1:6379> sismember set v3 (integer) 1 127.0.0.1:6379> sismember set v4 (integer) 0
127.0.0.1:6379> scard set (integer) 3
#srem 集合 元素 127.0.0.1:6379> srem set v2 (integer) 1 127.0.0.1:6379> SMEMBERS set 1) "v1" 2) "v3"
#SRANDMEMBER 集合 (个数) 127.0.0.1:6379> SRANDMEMBER set "v3" 127.0.0.1:6379> SRANDMEMBER set "v1"
#spop 集合 (个数) 127.0.0.1:6379> spop set "v3" 127.0.0.1:6379> spop set "v1" 127.0.0.1:6379> SMEMBERS set (empty array)
#smove 原集合 目标集合 原集合的value值 127.0.0.1:6379> sadd set v1 (integer) 1 127.0.0.1:6379> sadd set v2 (integer) 1 127.0.0.1:6379> sadd set v3 (integer) 1 127.0.0.1:6379> sadd set2 v4 #注意为set2 (integer) 1 127.0.0.1:6379> smove set set2 v3 (integer) 1 127.0.0.1:6379> SMEMBERS set 1) "v1" 2) "v2" 127.0.0.1:6379> SMEMBERS set2 1) "v4" 2) "v3"
都可以多个集合使用 sinter|sunion|sdiff set [set...]
127.0.0.1:6379> SMEMBERS set 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> SMEMBERS set2 1) "v4" 2) "v3" 127.0.0.1:6379> SINTER set set2 1) "v3" 127.0.0.1:6379> SUNION set set2 1) "v4" 2) "v1" 3) "v2" 4) "v3" 127.0.0.1:6379> sdiff set set2 #在前一个集合中,不在后面集合中的元素 1) "v1" 2) "v2"
hash key field(k) value
#hset key field value值 #hget key field 127.0.0.1:6379> flushdb OK 127.0.0.1:6379> hset hash f1 v1 (integer) 1 127.0.0.1:6379> hset hash f2 v2 (integer) 1 127.0.0.1:6379> hset hash f3 v3 (integer) 1 127.0.0.1:6379> hget hash f1 "v1"
127.0.0.1:6379> hmset hash f4 v4 f5 v5 OK 127.0.0.1:6379> hmget hash f1 f2 f3 f4 f5 1) "v1" 2) "v2" 3) "v3" 4) "v4" 5) "v5"
127.0.0.1:6379> hgetall hash 1) "f1" 2) "v1" 3) "f2" 4) "v2" 5) "f3" 6) "v3" 7) "f4" 8) "v4" 9) "f5" 10) "v5"
127.0.0.1:6379> hdel hash f1 (integer) 1 127.0.0.1:6379> hgetall hash 1) "f2" 2) "v2" 3) "f3" 4) "v3" 5) "f4" 6) "v4" 7) "f5" 8) "v5"
127.0.0.1:6379> hlen hash (integer) 4
#hexists key field 127.0.0.1:6379> hexists hash f1 (integer) 0 127.0.0.1:6379> hexists hash f2 (integer) 1
127.0.0.1:6379> hkeys hash 1) "f2" 2) "f3" 3) "f4" 4) "f5" 127.0.0.1:6379> HVALS hash 1) "v2" 2) "v3" 3) "v4" 4) "v5"
127.0.0.1:6379> hset hash f1 1 (integer) 1 127.0.0.1:6379> HINCRBY hash f1 2 (integer) 3 127.0.0.1:6379> hget hash f1 "3"
127.0.0.1:6379> hsetnx hash f5 v5 (integer) 0 127.0.0.1:6379> hsetnx hash f6 v6 (integer) 1
zset key score value
zrange 截取,和String用法相同
127.0.0.1:6379> flushdb OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379> zadd set 1 one (integer) 1 127.0.0.1:6379> zadd set 2 two 3 three (integer) 2 127.0.0.1:6379> zrange set 0 -1 1) "one" 2) "two" 3) "three"
#zrevrange key 开始位置 结束位置 127.0.0.1:6379> zrevrange set 0 -1 1) "three" 2) "two" 3) "one"
#zrangebyscore set 最小值 最大值 [显示包含score] [limit 从第几个开始 多少个] 127.0.0.1:6379> zrangebyscore set -inf +inf 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> zrangebyscore set -inf +inf withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 127.0.0.1:6379> zrangebyscore set -inf +inf withscores limit 0 2 1) "one" 2) "1" 3) "two" 4) "2"
127.0.0.1:6379> zrem set one (integer) 1 127.0.0.1:6379> ZRANGE set 0 -1 1) "two" 2) "three"
127.0.0.1:6379> zcard set (integer) 2
127.0.0.1:6379> zcount set 1 2 (integer) 1 127.0.0.1:6379> zcount set -inf +inf (integer) 2