1、获取符合规则的键名列表:keys 通配符
?:匹配任意一个字符
*:匹配任意个(>=0)字符
[]:匹配括号之间的任一字符,可以使用“-”表示范围
\x:匹配转义字符
2、exists key:判断一个键是否存在
3、del key [key1 key2]:删除键,返回删除的个数,>=1:删除成功,0:删除失败或者不存在
4、type key:返回键值的数据类型
5、incr new_num:自增+1
incrby new_num 10 : 自增+10
decr new_num:自减-1
decrby new_num 10:自减-10
incrbyfloat new_num 2.7 自增+2.7 浮点数
每次返回最新值,所有redis命令都是原子操作
6、key命名规则:对象类型:对象id:对象属性(user:1001:friends.list 用户id为1001的好友列表)
7、append key "hello" :向尾部追加值,返回总长度
8、strlen key:获取字符串总长度
9、同时获取或设置多个键值
127.0.0.1:6379[5]> mset aaa 111 bbb 222 OK 127.0.0.1:6379[5]> mget aaa bbb 1) "111" 2) "222"
10、散列类型
127.0.0.1:6379[5]> hset usermap name lucy (integer) 0 127.0.0.1:6379[5]> hmset usermap age 18 classes 3 address beijing OK 127.0.0.1:6379[5]> hget usermap name "lucy" 127.0.0.1:6379[5]> hmget usermap name age address 1) "lucy" 2) "18" 3) "beijing" 127.0.0.1:6379[5]> hgetall usermap 1) "name" 2) "lucy" 3) "age" 4) "18" 5) "classes" 6) "3" 7) "address" 8) "beijing" 127.0.0.1:6379[5]> hexists usermap name (integer) 1 127.0.0.1:6379[5]> hsetnx usermap name lili (integer) 0 127.0.0.1:6379[5]> hget usermap name "lucy" 127.0.0.1:6379[5]> hsetnx usermap love basketboll (integer) 1 127.0.0.1:6379[5]> hget usermap love "basketboll" 127.0.0.1:6379[5]> hincrby usermap age 5 (integer) 23 127.0.0.1:6379[5]> hget usermap age "23" 127.0.0.1:6379[5]> hincrby usermap age 7 (integer) 30 127.0.0.1:6379[5]> hget usermap age "30" 127.0.0.1:6379[5]> hdel usermap love address (integer) 2 127.0.0.1:6379[5]> hgetall usermap 1) "name" 2) "lucy" 3) "age" 4) "30" 5) "classes" 6) "3" 127.0.0.1:6379[5]>
11、列表类型
127.0.0.1:6379[5]> lpush userid_list 1 2 3 4 (integer) 4 127.0.0.1:6379[5]> rpush userid_list 5 6 7 8 (integer) 8 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "5" 6) "6" 7) "7" 8) "8" 127.0.0.1:6379[5]> lpop userid_list "4" 127.0.0.1:6379[5]> rpop userid_list "8" 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "3" 2) "2" 3) "1" 4) "5" 5) "6" 6) "7" 127.0.0.1:6379[5]> llen userid_list (integer) 6 127.0.0.1:6379[5]> lrange userid_list 0 2 1) "3" 2) "2" 3) "1" 127.0.0.1:6379[5]> lrange userid_list 1 -1 1) "2" 2) "1" 3) "5" 4) "6" 5) "7" 127.0.0.1:6379[5]> lrange userid_list 0 999 1) "3" 2) "2" 3) "1" 4) "5" 5) "6" 6) "7" 127.0.0.1:6379[5]> lrem userid_list 1 1 (integer) 1 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "3" 2) "2" 3) "5" 4) "6" 5) "7" 127.0.0.1:6379[5]> lpush userid_list 2 2 3 4 4 6 6 6 8 8 (integer) 15 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "8" 2) "8" 3) "6" 4) "6" 5) "6" 6) "4" 7) "4" 8) "3" 9) "2" 10) "2" 11) "3" 12) "2" 13) "5" 14) "6" 15) "7" 127.0.0.1:6379[5]> lrem userid_list 2 6 (integer) 2 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "8" 2) "8" 3) "6" 4) "4" 5) "4" 6) "3" 7) "2" 8) "2" 9) "3" 10) "2" 11) "5" 12) "6" 13) "7" 127.0.0.1:6379[5]> lrem userid_list -2 2 (integer) 2 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "8" 2) "8" 3) "6" 4) "4" 5) "4" 6) "3" 7) "2" 8) "3" 9) "5" 10) "6" 11) "7" 127.0.0.1:6379[5]> lrem userid_list 0 3 (integer) 2 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "8" 2) "8" 3) "6" 4) "4" 5) "4" 6) "2" 7) "5" 8) "6" 9) "7" 127.0.0.1:6379[5]> lindex userid_list 3 "4" 127.0.0.1:6379[5]> lset userid_list 1 99 OK 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "8" 2) "99" 3) "6" 4) "4" 5) "4" 6) "2" 7) "5" 8) "6" 9) "7" 127.0.0.1:6379[5]> ltrim userid_list 2 8 OK 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "6" 2) "4" 3) "4" 4) "2" 5) "5" 6) "6" 7) "7" 127.0.0.1:6379[5]> linsert userid_list before 2 10 (integer) 8 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "6" 2) "4" 3) "4" 4) "10" 5) "2" 6) "5" 7) "6" 8) "7" 127.0.0.1:6379[5]> linsert userid_list after 5 999 (integer) 9 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "6" 2) "4" 3) "4" 4) "10" 5) "2" 6) "5" 7) "999" 8) "6" 9) "7" 127.0.0.1:6379[5]> rpoplpush userid_list new_userid_list "7" 127.0.0.1:6379[5]> rpoplpush userid_list new_userid_list "6" 127.0.0.1:6379[5]> lrange userid_list 0 -1 1) "6" 2) "4" 3) "4" 4) "10" 5) "2" 6) "5" 7) "999" 127.0.0.1:6379[5]> lrange new_userid_list 0 -1 1) "6" 2) "7"
11、集合类型
127.0.0.1:6379[5]> sadd tagset 1 5 (integer) 2 127.0.0.1:6379[5]> sadd tagset 1 8 9 (integer) 2 127.0.0.1:6379[5]> smembers tagset 1) "1" 2) "5" 3) "8" 4) "9" 127.0.0.1:6379[5]> srem tagset 1 8 (integer) 2 127.0.0.1:6379[5]> smembers tagset 1) "5" 2) "9" 127.0.0.1:6379[5]> sismembers tagset 5 (error) ERR unknown command 'sismembers' 127.0.0.1:6379[5]> sismember tagset 5 (integer) 1 127.0.0.1:6379[5]> sismember tagset 10 (integer) 0 127.0.0.1:6379[5]> sadd setA 5 10 15 (integer) 3 127.0.0.1:6379[5]> sadd setB 5 7 8 (integer) 3 127.0.0.1:6379[5]> sadd setC 10 19 28 (integer) 3 127.0.0.1:6379[5]> sdiff setA setB setC 1) "15" 127.0.0.1:6379[5]> sinter setA setB setC (empty list or set) 127.0.0.1:6379[5]> sadd setC 5 (integer) 1 127.0.0.1:6379[5]> sinter setA setB setC 1) "5" 127.0.0.1:6379[5]> sunion setA setB setC 1) "5" 2) "7" 3) "8" 4) "10" 5) "15" 6) "19" 7) "28" 127.0.0.1:6379[5]> 127.0.0.1:6379[5]> 127.0.0.1:6379[5]> 127.0.0.1:6379[5]> sadd post:1:tags java (integer) 1 127.0.0.1:6379[5]> sadd post:2:tags mysql java (integer) 2 127.0.0.1:6379[5]> sadd post:3:tags java mysql redis (integer) 3 127.0.0.1:6379[5]> sadd tag:redis:posts 3 (integer) 1 127.0.0.1:6379[5]> sadd tag:mysql:posts 2 3 (integer) 2 127.0.0.1:6379[5]> sadd tag:java:posts 1 2 3 (integer) 3 127.0.0.1:6379[5]> smembers tag:redis:posts 1) "3" 127.0.0.1:6379[5]> smembers tag:java:posts 1) "1" 2) "2" 3) "3" 127.0.0.1:6379[5]> sinter tag:java:posts tag:mysql:posts 1) "2" 2) "3" 127.0.0.1:6379[5]> sinter tag:java:posts tag:mysql:posts tag:redis:posts 1) "3"
12、有序集合类型
127.0.0.1:6379[5]> zadd score 89 Tom 76 Lucy 66 zhangsan 98 shma (integer) 4 127.0.0.1:6379[5]> zadd score 75.5 sunwei (integer) 1 127.0.0.1:6379[5]> zscore score shma "98" 127.0.0.1:6379[5]> zrange score 0 999 widthscores (error) ERR syntax error 127.0.0.1:6379[5]> zrange score 0 -1 widthscores (error) ERR syntax error 127.0.0.1:6379[5]> zrange score 0 -1 1) "zhangsan" 2) "sunwei" 3) "Lucy" 4) "Tom" 5) "shma" 127.0.0.1:6379[5]> zrevrange score 0 -1 withscores 1) "shma" 2) "98" 3) "Tom" 4) "89" 5) "Lucy" 6) "76" 7) "sunwei" 8) "75.5" 9) "zhangsan" 10) "66" 127.0.0.1:6379[5]> zrange score 0 -1 withscores 1) "zhangsan" 2) "66" 3) "sunwei" 4) "75.5" 5) "Lucy" 6) "76" 7) "Tom" 8) "89" 9) "shma" 10) "98" 127.0.0.1:6379[5]> zrange score 10 100 (empty list or set) 127.0.0.1:6379[5]> zrangebyscore score 10 80 1) "zhangsan" 2) "sunwei" 3) "Lucy" 127.0.0.1:6379[5]> zrangebyscore score 10 80 widthscores (error) ERR syntax error 127.0.0.1:6379[5]> zrangebyscore score 10 80 withscores 1) "zhangsan" 2) "66" 3) "sunwei" 4) "75.5" 5) "Lucy" 6) "76" 127.0.0.1:6379[5]> zrevrangebyscore score (66 +inf (empty list or set) 127.0.0.1:6379[5]> zrangebyscore score (66 +inf 1) "sunwei" 2) "Lucy" 3) "Tom" 4) "shma" 127.0.0.1:6379[5]> zrangebyscore score -inf 98) (error) ERR min or max is not a float 127.0.0.1:6379[5]> zrangebyscore score (66 98 limit 1 2 1) "Lucy" 2) "Tom" 127.0.0.1:6379[5]> zincrby score 10 shma "108" 127.0.0.1:6379[5]> zcard score (integer) 5 127.0.0.1:6379[5]> zrem score Tom (integer) 1