一. set 类型数据操作指令简介
1. sadd : key member 添加一个 string 元素到 key 对应 set 集合中,成功返回 1,如果元素已经在集合中则返回 0,key 对应的 set 不存在则返回错误。
127.0.0.1:6379> KEYS * 1) "add_append" 2) "luo" 3) "mykey" 4) "num" 5) "fff" 6) "deng" 7) "myset" 8) "hello" 9) "wu" 10) "lang" 11) "ts" 127.0.0.1:6379> SADD test_sadd "test1_sadd" (integer) 1 127.0.0.1:6379> SMEMBERS test_sadd 1) "test1_sadd" 127.0.0.1:6379> SADD test_sadd "test2_sadd" (integer) 1 127.0.0.1:6379> SMEMBERS test_sadd 1) "test1_sadd" 2) "test2_sadd" 127.0.0.1:6379> SADD test_sadd "test3_sadd" (integer) 1 127.0.0.1:6379> SMEMBERS test_sadd 1) "test1_sadd" 2) "test2_sadd" 3) "test3_sadd" 127.0.0.1:6379>
2. smembers: key 返回 key 对应 set 的所有元素,结果是无序的。
127.0.0.1:6379> SMEMBERS test_sadd 1) "test1_sadd" 2) "test2_sadd" 3) "test3_sadd" 127.0.0.1:6379>
3. srem :key member 从 key 对应 set 中移除指定元素,成功返回 1,如果 member 在集合中不存在或者 key 不存在返回 0,如果 key 对应的不是 set 类型的值返回错误。
127.0.0.1:6379> SADD test_srem "one" (integer) 1 127.0.0.1:6379> SADD test_srem "two" (integer) 1 127.0.0.1:6379> SADD test_srem "three" (integer) 1 127.0.0.1:6379> SADD test_srem "four" (integer) 1 127.0.0.1:6379> SADD test_srem "five" (integer) 1 127.0.0.1:6379> SADD test_srem "six" (integer) 1 127.0.0.1:6379> SADD test_srem "seven" (integer) 1 127.0.0.1:6379> SADD test_srem "eight" (integer) 1 127.0.0.1:6379> SADD test_srem "nine" (integer) 1 127.0.0.1:6379> SADD test_srem "ten" (integer) 1 #移出成功返回1 127.0.0.1:6379> SMEMBERS test_srem 1) "eight" 2) "two" 3) "seven" 4) "four" 5) "six" 6) "five" 7) "one" 8) "ten" 9) "three" 10) "nine" 127.0.0.1:6379> SREM test_srem ‘four‘ (integer) 1 127.0.0.1:6379> SMEMBERS test_srem 1) "seven" 2) "six" 3) "five" 4) "one" 5) "ten" 6) "two" 7) "eight" 8) "three" 9) "nine" # ten所对应的set集合中的值确实被srem掉了. 127.0.0.1:6379>
127.0.0.1:6379> SREM test_srem ‘elenen‘
(integer) 0 # 移出不成功则返回0
127.0.0.1:6379>
4. spop :key 删除并返回 key 对应 set 中随机的一个元素,如果 set 是空或者 key 不存在返回
nil。
127.0.0.1:6379> SMEMBERS test_srem 1) "six" 2) "five" 3) "one" 4) "ten" 5) "two" 6) "eight" 7) "three" 8) "nine" 9) "seven" 127.0.0.1:6379> SPOP (error) ERR wrong number of arguments for ‘spop‘ command 127.0.0.1:6379> SPOP test_srem "five" # 随机删除"five" 127.0.0.1:6379> SPOP test_srem "nine" #随机删除"nine"127.0.0.1:6379> SMEMBERS test_srem 1) "ten" 2) "two" 3) "eight" 4) "six" 5) "three" 6) "seven" 7) "one" 127.0.0.1:6379>
5. srandmember : key 同 spop,随机取 set 中的一个元素,但是不删除元素。
127.0.0.1:6379> SMEMBERS t 1) "three" 2) "ten" 3) "two" 4) "nine" 5) "eight" 6) "six" 7) "five" 8) "one" 9) "four" 127.0.0.1:6379> SRANDMEMBER t "three" 127.0.0.1:6379> SRANDMEMBER t "three" 127.0.0.1:6379> SRANDMEMBER t "ten" #只是随机取出来,但是没有进行删除,这是区别于spop的一个点 127.0.0.1:6379> SMEMBERS t 1) "six" 2) "five" 3) "one" 4) "ten" 5) "eight" 6) "two" 7) "three" 8) "nine" 9) "four" 127.0.0.1:6379> ]
6. smove :srckey dstkey member 从 srckey 对应 set 中移除 member 并添加到 dstkey 对应 set 中,整个操作是原子的。成功返回 1,如果 member 在 srckey 中不存在返回 0,如果 key 不是 set类型返回错误。
127.0.0.1:6379> SMEMBERS myset 1) "world" 2) "Hello" 3) "hello" 4) "World_world" 5) "test_scard" 6) "World" 127.0.0.1:6379> SMEMBERS t 1) "six" 2) "five" 3) "one" 4) "ten" 5) "eight" 6) "two" 7) "three" 8) "nine" 9) "four" 127.0.0.1:6379> SMOVE t myset "nine" # 移出元素的语句 (integer) 1 127.0.0.1:6379> SMEMBERS t 1) "five" 2) "one" 3) "ten" 4) "eight" 5) "two" 6) "six" 7) "three" 8) "four" # 少了移出的元素nine 127.0.0.1:6379> SMEMBERS myset 1) "hello" 2) "Hello" 3) "world" 4) "World_world" 5) "nine" # 这条记录是在set集合 t中smove过来的 6) "test_scard" 7) "World" 127.0.0.1:6379>
7. scard key 返回 set 的元素个数,如果 set 是空或者 key 不存在返回 0。
127.0.0.1:6379> SMEMBERS t 1) "five" 2) "one" 3) "ten" 4) "eight" 5) "two" 6) "six" 7) "three" 8) "four" 127.0.0.1:6379> SCARD t # 查看set集合元素条数 (integer) 8 127.0.0.1:6379>
8. sismember: key member 判断 member 是否在 set 中,存在返回 1,0 表示不存在或者 key 不存在。
127.0.0.1:6379> SMEMBERS t 1) "five" 2) "one" 3) "ten" 4) "eight" 5) "two" 6) "six" 7) "three" 8) "four" 127.0.0.1:6379> SISMEMBER t ‘two‘ # 存在元素返回1 (integer) 1 127.0.0.1:6379> SISMEMBER t ‘twott‘ # 不存在返回0 (integer) 0 127.0.0.1:6379>
9. sinter : key1 key2 ...... keyN 返回所有给定 key 的交集。
127.0.0.1:6379> SMEMBERS t 1) "ten" 2) "eight" 3) "two" 4) "six" 5) "five" 6) "three" 7) "four" 8) "one" 127.0.0.1:6379> SMEMBERS test_srem 1) "ten" 2) "two" 3) "eight" 4) "six" 5) "three" 6) "seven" 7) "one" 127.0.0.1:6379> SINTER t test_srem # 返回两个set的交集 1) "ten" 2) "two" 3) "eight" 4) "six" 5) "three" 6) "one" 127.0.0.1:6379>
10. sismember : key member 判断 member 是否在 set 中,存在返回 1,0 表示不存在或者 key 不存在。
127.0.0.1:6379> 127.0.0.1:6379> SMEMBERS t 1) "ten" 2) "eight" 3) "two" 4) "six" 5) "five" 6) "three" 7) "four" 8) "one" 127.0.0.1:6379> SMEMBERS test_srem 1) "ten" 2) "two" 3) "eight" 4) "six" 5) "three" 6) "seven" 7) "one" 127.0.0.1:6379> SINTERSTORE tt t test_srem (integer) 6 127.0.0.1:6379> SMEMBERS tt # 查看是否保存成功,将set集合t和set集合test_srem的交集保存到新的set集合tt中 1) "ten" 2) "eight" 3) "two" 4) "three" 5) "six" 6) "one" 127.0.0.1:6379>
11. sunion: key1 key2 ...... keyN 返回所有给定 key 的并集
127.0.0.1:6379> SMEMBERS t 1) "ten" 2) "eight" 3) "two" 4) "six" 5) "five" 6) "three" 7) "four" 8) "one" 127.0.0.1:6379> SMEMBERS myset 1) "hello" 2) "Hello" 3) "world" 4) "World_world" 5) "nine" 6) "test_scard" 7) "World" 127.0.0.1:6379> SUNION t myset # 这就是sunion后的结果,相当于将所有的数据合并. 1) "one" 2) "world" 3) "ten" 4) "two" 5) "eight" 6) "World" 7) "six" 8) "five" 9) "World_world" 10) "Hello" 11) "three" 12) "nine" 13) "hello" 14) "test_scard" 15) "four" 127.0.0.1:6379>
12. sunionstore : dstkey key1 ...... keyN 返回所有给定 key 的并集,并保存并集到 dstkey 下。
127.0.0.1:6379> SMEMBERS t 1) "ten" 2) "eight" 3) "two" 4) "six" 5) "five" 6) "three" 7) "four" 8) "one" 127.0.0.1:6379> SMEMBERS myset 1) "hello" 2) "Hello" 3) "world" 4) "World_world" 5) "nine" 6) "test_scard" 7) "World" 127.0.0.1:6379> SUNIONSTORE ttt tt myset # 将合并的数据保存在ttt set集合中 (integer) 13 127.0.0.1:6379> SMEMBERS ttt 1) "one" 2) "world" 3) "ten" 4) "two" 5) "eight" 6) "World" 7) "six" 8) "World_world" 9) "three" 10) "Hello" 11) "hello" 12) "nine" 13) "test_scard" 127.0.0.1:6379>
13. sdiff : key1 key2 ...... keyN 返回所有给定 key 的差集。
127.0.0.1:6379> SMEMBERS t 1) "ten" 2) "eight" 3) "two" 4) "six" 5) "five" 6) "three" 7) "four" 8) "one" 127.0.0.1:6379> SMEMBERS myset 1) "hello" 2) "Hello" 3) "world" 4) "World_world" 5) "nine" 6) "test_scard" 7) "World" 127.0.0.1:6379> SDIFF t myset # 返回的差集 1) "three" 2) "ten" 3) "eight" 4) "two" 5) "four" 6) "six" 7) "five" 8) "one" 127.0.0.1:6379>
14. sdiffstore : dstkey key1 ...... keyN 返回所有给定 key 的差集,并保存差集到 dstkey 下。
127.0.0.1:6379> SMEMBERS t 1) "ten" 2) "eight" 3) "two" 4) "six" 5) "five" 6) "three" 7) "four" 8) "one" 127.0.0.1:6379> SMEMBERS myset 1) "Hello" 2) "world" 3) "World_world" 4) "hello" 5) "nine" 6) "test_scard" 7) "World" 127.0.0.1:6379> SDIFFSTORE t_sdiffstore t myset # 生成差集数据并保存到set t_sdiffstore内 (integer) 8 127.0.0.1:6379> SMEMBERS t_sdiffstore # 查看差集数据 1) "three" 2) "ten" 3) "eight" 4) "two" 5) "four" 6) "six" 7) "five" 8) "one" 127.0.0.1:6379>
Redis 的 Key:
Redis 的 key 是字符串类型,但是 key 中不能包括边界字符,由于 key 不是 binary safe
的字符串,所以像"my key"和"mykey\n"这样包含空格和换行的 key 是不允许的。
二, key相关指令:
1. exits : key 检测指定 key 是否存在,返回 1 表示存在,0 不存在
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "wu" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> EXISTS wu # 存在就返回1 (integer) 1 127.0.0.1:6379> EXISTS myset (integer) 1 127.0.0.1:6379> EXISTS mysetttt # 不存在就返回0 (integer) 0 127.0.0.1:6379>
2. del : key1 key2 ...... keyN 删除给定 key,返回删除 key 的数目,0 表示给定 key 都不存在
127.0.0.1:6379> SET key1 "hello"
OK
127.0.0.1:6379> SET key2 "hello"
OK
127.0.0.1:6379> KEYS * # 添加key,key2之后的set 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "key2" 11) "fff" 12) "key1" 13) "myset" 14) "t_sdiffstore" 15) "wu" 16) "lang" 17) "test_srem" 18) "t" 19) "test_sadd" 127.0.0.1:6379> del key1 key2 key3 # 删除key1,2的动作 (integer) 2 127.0.0.1:6379> KEYS * # 删除key1和key2之后的keys 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "wu" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379>
3. type : key 返回给定 key 值的类型。返回 none 表示 key 不存在,string 字符类型,list 链表类型 set 无序集合类型......
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "wu" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> TYPE wu # wu为string类型 string 127.0.0.1:6379> TYPE myset set 127.0.0.1:6379> TYPE t # t为set类型 set 127.0.0.1:6379>
4. keys : pattern 返回匹配指定模式的所有 key
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "wu" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379>
5. randomkey: 返回从当前数据库中随机选择的一个 key,如果当前数据库是空的,返回空 串
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "wu" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> RANDOMKEY "deng" 127.0.0.1:6379> RANDOMKEY "luo" 127.0.0.1:6379> RANDOMKEY "test_sadd" 127.0.0.1:6379>
6. rename : oldkey newkey 重命名一个 key,如果 newkey 存在,将会被覆盖,返回 1 表示成功,0 失败。可能是 oldkey 不存在或者和 newkey 相同。
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "deng" 5) "hello" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "wu" # 要覆盖的内容 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> RENAME wu WU OK 127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "WU" # 覆盖后的内容 5) "deng" 6) "hello" 7) "ttt" 8) "ts" 9) "tt" 10) "luo" 11) "fff" 12) "myset" 13) "t_sdiffstore" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379>
7. renamenx oldkey newkey 同上,但是如果 newkey 存在返回失败。
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "WU" 5) "deng" 6) "hello" 7) "ttt" 8) "ts" 9) "tt" 10) "luo" 11) "fff" 12) "myset" 13) "t_sdiffstore" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> RENAMENX WU wuyanlong (integer) 1 127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "wuyanlong" 5) "deng" 6) "hello" 7) "ttt" 8) "ts" 9) "tt" 10) "luo" 11) "fff" 12) "myset" 13) "t_sdiffstore" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> RENAMENX wuyanlong wuyanlong (error) ERR source and destination objects are the same # 要覆盖的内容存在所以返回失败 127.0.0.1:6379>
8. expire key seconds 为 key 指定过期时间,单位是秒。返回 1 成功,0 表示 key 已经设置过过期时间或者不存在。
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "wuyanlong" 5) "deng" 6) "hello" 7) "ttt" 8) "ts" 9) "tt" 10) "luo" 11) "fff" 12) "myset" 13) "t_sdiffstore" 14) "lang" 15) "test_srem" 16) "t" 17) "test_sadd" 127.0.0.1:6379> EXPIRE hello 20 # 为key "hello"指定过期时间是20s (integer) 1 127.0.0.1:6379> KEYS * # 20s之后查看,hello key已经消失 1) "add_append" 2) "mykey" 3) "num" 4) "wuyanlong" 5) "deng" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "lang" 14) "test_srem" 15) "t" 16) "test_sadd" 127.0.0.1:6379>
9. ttl key 返回设置过过期时间 key 的剩余过期秒数。-1 表示 key 不存在或者未设置过期时间。
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "wuyanlong" 5) "deng" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "lang" 14) "test_srem" 15) "t" 16) "test_sadd" 127.0.0.1:6379> 127.0.0.1:6379> 127.0.0.1:6379> TTL (error) ERR wrong number of arguments for ‘ttl‘ command 127.0.0.1:6379> TTL wuyanlong (integer) -1 127.0.0.1:6379> EXPIRE wuyanlong 100000000 # 设置过期时间 (integer) 1 127.0.0.1:6379> TTL wuyanlong # 查看过期时间 (integer) 99999991 127.0.0.1:6379> TTL wuyanlong (integer) 99999987 127.0.0.1:6379>
10 . select db-index 通过索引选择数据库,默认连接的数据库是 0,默认数据库数是 16 个。返回 1表示成功,0 失败。
127.0.0.1:6379> SELECT 0 OK 127.0.0.1:6379> SELECT 2 OK 127.0.0.1:6379[2]> SMEMBERS citycodeid (empty list or set) 127.0.0.1:6379[2]> KEYS * 1) "pcity:110100" 2) "www:cache:nav:330200" 3) "citycodeid:fz" 4) "www:cache:nav:620100" 5) "citycodeid:xa" 6) "citycodeid:suzhou" 7) "citycodeid:nj" 8) "citycodeid:km" 9) "citymtimecity:950" 10) "citymtimecity:328" 11) "cityyonglecity:yl650000" 12) "citymtimecity:992" 13) "cityyonglecity:yl315000"...........
11. move key db-index 将 key 从当前数据库移动到指定数据库。返回 1 表示成功。0 表示 key不存在或者已经在指定数据库中。
12. DUMP key序列化给定 key ,并返回被序列化的值,使用 RESTORE 命令可以将这个值反序列化为 Redis 键。
127.0.0.1:6379> KEYS * 1) "add_append" 2) "mykey" 3) "num" 4) "wuyanlong" 5) "deng" 6) "ttt" 7) "ts" 8) "tt" 9) "luo" 10) "fff" 11) "myset" 12) "t_sdiffstore" 13) "lang" 14) "test_srem" 15) "t" 16) "test_sadd" 127.0.0.1:6379> DUMP wuyanlong "\x00\x0cyanlongyanzu\x06\x00\xaabS7\xad\xb6X\xb9" 127.0.0.1:6379> DUMP fff "\x02\x01\x03sss\x06\x00\xc3\xd8\x16\x8c]\x8c\xfc\xf7" 127.0.0.1:6379> RESTORE fff "\x02\x01\x03sss\x06\x00\xc3\xd8\x16\x8c]\x8c\xfc\xf7"