Redis数据类型
-
通用命令
-
KEYS
查看所有符合模板的keys,损耗性能不建议使用 -
DEL
删除n个指定key -
EXISTS
查看指定key是否操作 -
EXPIRE
设置有效期,单位s -
TTL
查询剩余有效期为-1时表示永久有效
-
-
String
类型SET
添加或者修改已经存在的的键值对SET key value
SET key value [nx] [ex [expire]]
GET
获取指定key的valueMSET
批量添加多个Strinf类型的键值对MGET
获取多个key的valueINCR
让整型类型自增1INCRBY
让整型类型自增指定步长INCRBYFLOAT
让浮点类型自增指定步长SETNX
添加一个不存在的key,如果key存在,则不进行任何操作SETEX
添加指定key的value,并且设置有效期SETEX key expire value
-
Hash
类型常见命令
HSET
添加或修改指定key的field的值HGET
获取指定key的field的值HMSET
批量HMGET
批量HGETALL
获取指定key所有的field和valueHKEYS
获取指定key所有的fieldHVALS
获取指定key所有的valueHINCRBY
指定key的指定field自增指定步长HSETNX
指定key的field是否存在,不存在则添加
-
List
类型运行测试
127.0.0.1:6379[1]> LPUSH user a b c (integer) 3 127.0.0.1:6379[1]> RPUSH user x y z (integer) 6 127.0.0.1:6379[1]>
127.0.0.1:6379[1]> LPOP user "c" 127.0.0.1:6379[1]> RPOP user "z"
-
Set
类型练习测试
127.0.0.1:6379[1]> SADD s1 a b c 9 8 7 (integer) 6 127.0.0.1:6379[1]> SADD s2 b c 9 8 x y z (integer) 7 127.0.0.1:6379[1]> SMEMBERS s1 1) "c" 2) "b" 3) "a" 4) "8" 5) "9" 6) "7" 127.0.0.1:6379[1]> SREM s1 a b (integer) 2 127.0.0.1:6379[1]> SMEMBERS s1 1) "c" 2) "8" 3) "9" 4) "7" 127.0.0.1:6379[1]> SCARD s1 (integer) 4 127.0.0.1:6379[1]> SISMEMBER s1 a (integer) 0 127.0.0.1:6379[1]> SISMEMBER s1 c (integer) 1 127.0.0.1:6379[1]> SINTER s1 s2 1) "c" 2) "8" 3) "9" 127.0.0.1:6379[1]> SDIFF s1 s2 1) "7" 127.0.0.1:6379[1]> SDIFF s2 s1 1) "y" 2) "z" 3) "x" 4) "b" 127.0.0.1:6379[1]> SUNION s1 s2 1) "x" 2) "c" 3) "8" 4) "b" 5) "y" 6) "z" 7) "9" 8) "7"
SDIFF key1 key2
比较的是key1中,key2所没有的,两者对调,结果不同127.0.0.1:6379[1]> SADD zhangsan lisi wangwu zhaoliu (integer) 3 127.0.0.1:6379[1]> SADD lisi wangwu mazi ergou (integer) 3 127.0.0.1:6379[1]> SCARD zhangsan (integer) 3 127.0.0.1:6379[1]> SINTER zhangsan lisi 1) "wangwu" 127.0.0.1:6379[1]> SDIFF zhangsan lisi 1) "zhaoliu" 2) "lisi" 127.0.0.1:6379[1]> SUNION zhangsan lisi 1) "zhaoliu" 2) "lisi" 3) "wangwu" 4) "ergou" 5) "mazi" 127.0.0.1:6379[1]> SISMEMBER zhangsan lisi (integer) 1 127.0.0.1:6379[1]> SISMEMBER lisi zhangsan (integer) 0 127.0.0.1:6379[1]> SREM zhangsan lisi (integer) 1
-
SortedSet
类型底层与
Set
差异较大测试练习
127.0.0.1:6379[1]> ZADD stus 85 Jack 89 Lucy 82 Rose 95 Tom 78 Jerry 92 Amy 76 Miles (integer) 7 127.0.0.1:6379[1]> ZREM stus Tom (integer) 1 127.0.0.1:6379[1]> ZSCORE stus Amy "92" 127.0.0.1:6379[1]> ZRANK stus Rose (integer) 2 127.0.0.1:6379[1]> ZCOUNT stus 0 80 (integer) 2 127.0.0.1:6379[1]> ZINCRBY stus 2 Amy "94" 127.0.0.1:6379[1]> ZREVRANGE stus 0 2 1) "Amy" 2) "Lucy" 3) "Jack" 127.0.0.1:6379[1]> ZRANGEBYSCORE stus 0 80 1) "Miles" 2) "Jerry"
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。