redis命令操作方式:


一. 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"

 

三, string 类型:string 类型数据操作指令简介

  string 是最基本的类型,而且 string 类型是二进制安全的。意思是 redis 的 string 可以
包含任何数据。比如 jpg 图片或者序列化的对象。从内部实现来看其实 string 可以看作 byte
3数组,最大上限是 1G 字节。

 1. set key value 设置 key 对应 string 类型的值,返回 1 表示成功,0 失败。

127.0.0.1:6379> SET test1 "luoting"
OK
127.0.0.1:6379> GET test1
"luoting"
127.0.0.1:6379> 

 

2.  setnx key value 如果 key 不存在,设置 key 对应 string 类型的值。如果 key 已经存在,返回 0。

127.0.0.1:6379> KEYS *
 1) "test1"
 2) "ts"
 3) "tt"
 4) "myset"
 5) "fff"
 6) "deng"
 7) "add_append"
 8) "test_sadd"
 9) "t"
10) "num"
11) "t_sdiffstore"
12) "mykey"
13) "ttt"
14) "wuyanlong"
15) "luo"
16) "test_srem"
17) "lang"
127.0.0.1:6379> SETNX test1 "hellow0rld"    # test1已经存在所以返回0
(integer) 0
127.0.0.1:6379> SETNX test2 "helloworld"    # test2不存在所以返回1
(integer) 1
127.0.0.1:6379> 

 

3. get key 获取 key 对应的 string 值,如果 key 不存在返回 nil

127.0.0.1:6379> GET test1  # test1存在
"luoting"
127.0.0.1:6379> GET test2  # test2存在
"helloworld"
127.0.0.1:6379> GET test3  # test3不存在
(nil)
127.0.0.1:6379> 

 

4. getset key value 先获取 key 的值,再设置 key 的值。如果 key 不存在返回 nil。

127.0.0.1:6379> GETSET test1 "test_getset"
"luoting"
127.0.0.1:6379> GET test1
"test_getset"
127.0.0.1:6379> GETSET test3 "test_getset"
(nil)
127.0.0.1:6379> 

 

5. mget key1 key2 ...... keyN 一次获取多个 key 的值,如果对应 key 不存在,则对应返回 nil。

127.0.0.1:6379> MGET test1 test2 test3 test4
1) "test_getset"
2) "helloworld"
3) "test_getset"
4) (nil)
127.0.0.1:6379> 

 

 

6. mset key1 value1 ...... keyN valueN 一次设置多个 key 的值,成功返回 1 表示所有的值都设置了,失败返回 0 表示没有任何值被设置。

127.0.0.1:6379> MSET key1 "test1" key2 "test2"
OK
127.0.0.1:6379> get key1
"test1"
127.0.0.1:6379> get key2
"test2"
127.0.0.1:6379> MSET key1 "test1" key2 "test2"
OK
127.0.0.1:6379> 

 

7. msetnx key1 value1 ...... keyN valueN 一次设置多个 key 的值,但是不会覆盖已经存在的 key

127.0.0.1:6379> MSETNX key1 "hello" key2 "helloo" key3 "hellooo" key4 "heloooo"  # key1 key2 已经存在了,所以设置失败
(integer) 0
127.0.0.1:6379> GET key4
(nil)
127.0.0.1:6379> GET key1
"test1"
127.0.0.1:6379> MSETNX key3 "hello" key4 "helloo" key5 "hellooo"    # key3 key4 key5 不存在所以设置成功
(integer) 1
127.0.0.1:6379> get key3 
"hello"127.0.0.1:6379> 

 

 

8. incr key: 对 key 的值做++操作,并返回新的值。注意 incr 一个不是 int 的 value 会返回错误,incr 一个不存在的 key,则设置 key 值为 1。

127.0.0.1:6379> INCR kk
(integer) 1
127.0.0.1:6379> GET kk
"1"
127.0.0.1:6379> INCR kk
(integer) 2
127.0.0.1:6379> incr kk
(integer) 3
127.0.0.1:6379> get kk
"3"
127.0.0.1:6379> get key1
"test1"
127.0.0.1:6379> 

 

9. decr key 对 key: 的值做--操作,decr 一个不存在 key,则设置 key 值为-1。

127.0.0.1:6379> DECR key1
(error) ERR value is not an integer or out of range
127.0.0.1:6379> GET k_k
(nil)
127.0.0.1:6379> DECR k_k
(integer) -1
127.0.0.1:6379> DECR k_k
(integer) -2
127.0.0.1:6379> DECR k_k
(integer) -3
127.0.0.1:6379> GET k_k
"-3"
127.0.0.1:6379> 

 

10.  incrby key integer 对 key 加上指定值 ,key 不存在时候会设置 key,并认为原来的 value是 0。

127.0.0.1:6379> get key1
"test1"
127.0.0.1:6379> INCRBY key1 9
(error) ERR value is not an integer or out of range
127.0.0.1:6379> INCRBY key1 "hello"
(error) ERR value is not an integer or out of range
127.0.0.1:6379> INCRBY tt_t 99
(integer) 99
127.0.0.1:6379> GET tt_t
"99"
127.0.0.1:6379> INCRBY tt_t 99
(integer) 198
127.0.0.1:6379> 

 

11.  decrby key integer 对 key 减去指定值。decrby 完全是为了可读性,我们完全可以通过 incrby一个负值来实现同样效果,反之一样。

127.0.0.1:6379> GET key1 
"test1"
127.0.0.1:6379> DECR key1 90
(error) ERR wrong number of arguments for 'decr' command
127.0.0.1:6379> DECRBY key1 90
(error) ERR value is not an integer or out of range
127.0.0.1:6379> DECRBY key11 90
(integer) -90
127.0.0.1:6379> DECRBY key11 90
(integer) -180
127.0.0.1:6379> GET key11
"-180"
127.0.0.1:6379> 

12 strlen: 返回key所对应值的长度如果键存在的情况下, 如果key对应的不是一个string值类型,则返回错误.

127.0.0.1:6379> get key1
"test1"
127.0.0.1:6379> SMEMBERS myset
1) "World_world"
2) "world"
3) "World"
4) "nine"
5) "hello"
6) "test_scard"
7) "Hello"
127.0.0.1:6379> STRLEN myset  # myset不是一个string而是一个set集合, 因此返错误信息
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> 

 

13 . SETRANGE key offset value  替换操作

127.0.0.1:6379> SET key "hello world"
OK
127.0.0.1:6379> GET key
"hello world"
127.0.0.1:6379> SETRANGE key 6 "redis"
(integer) 11
127.0.0.1:6379> GET key
"hello redis"
127.0.0.1:6379> SETRANGE keyk 8 "redis_test"  # 如果key不存在,那么指定的位置也不存在,因此SETRANGE会对设置的key进行补零操作.
(integer) 18
127.0.0.1:6379> get keyk  
"\x00\x00\x00\x00\x00\x00\x00\x00redis_test"
127.0.0.1:6379> 

 

14 . SETEX:   Set key to hold the string value and set key to timeout after a given number of seconds. This command is equivalent to executing the following commands

这个命令相当于以下两条命令的集合:

SET mykey value
EXPIRE mykey seconds
127.0.0.1:6379> GET h1
(nil)
127.0.0.1:6379> SETEX h1 10 "test_setex"
OK
127.0.0.1:6379> GET h1
"test_setex"
127.0.0.1:6379> GET h1  # ten secends latter
(nil)
127.0.0.1:6379> 

以上两条命令的操作:

127.0.0.1:6379> get h2
(nil)
127.0.0.1:6379> set h2 "test_setex"
OK
127.0.0.1:6379> get h2
"test_setex"
127.0.0.1:6379> EXPIRE h2 10
(integer) 1
127.0.0.1:6379> get h2
"test_setex"
127.0.0.1:6379> get h2
"test_setex"
127.0.0.1:6379> get h2
(nil)
127.0.0.1:6379> 

 

15 PSETEX:       psetex和setex是功能基本是一样的,只是psetex设置的是毫秒,setex设置的是秒为单位

127.0.0.1:6379> PSETEX h3 10000 "test_psetex"
OK
127.0.0.1:6379> get h3
"test_psetex"
127.0.0.1:6379> get h3
"test_psetex"
127.0.0.1:6379> get h3  # 10s   =  10000ms之后该值消失
(nil)
127.0.0.1:6379> 

 

16 APPEND :   If key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case.

127.0.0.1:6379> 
127.0.0.1:6379> SET append "test_append"
OK
127.0.0.1:6379> GET append
"test_append"
127.0.0.1:6379> get key1
"test1"
127.0.0.1:6379> APPEND key1 "add_some_thing"
(integer) 19
127.0.0.1:6379> get key1
"test1add_some_thing"
127.0.0.1:6379> 

 

17. BITCOUNT key [start end]:Count the number of set bits (population counting) in a string.

 

127.0.0.1:6379> get key1
"test1add_some_thing"
127.0.0.1:6379> BITCOUNT key1
(integer) 82
127.0.0.1:6379> BITCOUNT key1 0 0 
(integer) 4
127.0.0.1:6379> BITCOUNT key1 0 6
(integer) 26
127.0.0.1:6379>

 

18 . BITOP operation destkey key [key ...] Perform a bitwise operation between multiple keys (containing string values) and store the result in the destination key.

 

The BITOP command supports four bitwise operations: AND, OR, XOR and NOT, thus the valid forms to call the command are:

  • BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyN
  • BITOP OR destkey srckey1 srckey2 srckey3 ... srckeyN
  • BITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyN
  • BITOP NOT destkey srckey

As you can see NOT is special as it only takes an input key, because it performs inversion of bits so it only makes sense as an unary operator.

The result of the operation is always stored at destkey.

127.0.0.1:6379> BITOP and dest key1 key2
(integer) 19
127.0.0.1:6379> get dest
"test0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
127.0.0.1:6379> get key1 
"test1add_some_thing"
127.0.0.1:6379> get key2
"test2"
127.0.0.1:6379> 

 

19. BITPOS key bit [start] [end]

20. GETBIT key offset:  

Returns the bit value at offset in the string value stored at key.

 

When offset is beyond the string length, the string is assumed to be a contiguous space with 0 bits. When key does not exist it is assumed to be an empty string, sooffset is always out of range and the value is also assumed to be a contiguous space with 0 bits.

 

四 hash 类型:

hash 是一个 string 类型的 field 和 value 的映射表。添加,删除操作都是 O(1)(平均)。hash 特别适合用于存储对象。相对于将对象的每个字段存成单个 string 类型。将一个对象存储在 hash 类型中会占用更少的内存,并且可以更方便的存取整个对象。省内存的原因是新建一个 hash 对象时开始是用 zipmap(又称为 small hash)来存储的。这个 zipmap 其实并不是 hash table,但zipmap 相比正常的 hash 实现可以节省不少 hash 本身需要的一些元数据存储开销。尽管 zipmap 的添加,删除,查找都是 O(n),但是由于一般对象的 field数量都不太多。所以使用 zipmap 也是很快的,就是说添加删除平均还是 O(1)。如果 field或者 value 的大小超出一定限制后,redis 会在内部自动将 zipmap 替换成正常的 hash 实现.这个限制可以在配置文件中指定。
hash-max-zipmap-entries 64#配置字段最多 64 个
hash-max-zipmap-value 512#配置 value 最大为 512 字节

 

hash 类型数据操作指令简介:

1.hset key field value :设置 hash field 为指定值,如果 key 不存在,则创建

 

127.0.0.1:6379> HSET hash_key field1 "hello"
(integer) 1127.0.0.1:6379> HGET hash_key field1
"hello"127.0.0.1:6379> HSET hash_key field2 "world" 
(integer) 1
127.0.0.1:6379> type hash_key
hash127.0.0.1:6379> 

 

2. hget key field 获取指定的 hash field。

127.0.0.1:6379> GET hash_key  # 错误的获取hash值的方式
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> HGET hash_key  # 错误的获取hash值方式
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> HGET hash_key field1  # 正确的获取hash值的方式
"hello"
127.0.0.1:6379> HGET hash_key field2
"world"
127.0.0.1:6379> 

 

3. hmget key filed1....fieldN 获取全部指定的 hash filed。

127.0.0.1:6379> HMGET hash_key field1 field2
1) "hello"
2) "world"
127.0.0.1:6379> 

 

 

4. hmset key filed1 value1 ...... filedN valueN 同时设置 hash 的多个 field。

127.0.0.1:6379> HSET hash_key field3 "test1" field4 "test2" field5 "test3"
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379> HMSET hash_key field3 "test1" field4 "test2" field5 "test3"
OK127.0.0.1:6379> HMGET hash_key field1 field2 field3 field4 field5
1) "hello"
2) "world"
3) "test1"
4) "test2"
5) "test3"
127.0.0.1:6379> 

 

5. hincrby key field integer 将指定的 hash filed 加上指定值。成功返回 hash filed 变更后的值。

127.0.0.1:6379> HGET hash_key field5
"test3"
127.0.0.1:6379> HINCRBY hash_key field5 1  # 当不是int类型的时候返回错误
(error) ERR hash value is not an integer
127.0.0.1:6379> HSET hash_key field6 100
(integer) 1
127.0.0.1:6379> HGET hash_key field6
"100"
127.0.0.1:6379> HINCRBY hash_key field6 11
(integer) 111
127.0.0.1:6379> HGET hash_key field6
"111"
127.0.0.1:6379> 

 

 

6. hexists key field 检测指定 field 是否存在。

127.0.0.1:6379> HEXISTS hash_key field6  # 存在返回1
(integer) 1
127.0.0.1:6379> HEXISTS hash_key field7  # 不存在返回0
(integer) 0
127.0.0.1:6379> 

 

7. hdel key field 删除指定的 hash field。

127.0.0.1:6379> HMGET hash_key field1 field2 field3 field4 field5 field6  # 未删除之前
1) "hello"
2) "world"
3) "test1"
4) "test2"
5) "test3"
6) "111"
127.0.0.1:6379> HDEL hash_key field1
(integer) 1
127.0.0.1:6379> HMGET hash_key field1 field2 field3 field4 field5 field6  # 删除之后
1) (nil)
2) "world"
3) "test1"
4) "test2"
5) "test3"
6) "111"
127.0.0.1:6379> 

 

8. hlen key 返回指定 hash 的 field 数量。

127.0.0.1:6379> HMGET hash_key field1 field2 field3 field4 field5 field6
1) (nil)
2) "world"
3) "test1"
4) "test2"
5) "test3"
6) "111"
127.0.0.1:6379> HLEN hash_key
(integer) 5
127.0.0.1:6379> 

 

9. hkeys key: 返回 hash 的所有 field。

127.0.0.1:6379> HSET hash_key1  field1 "100"
(integer) 1
127.0.0.1:6379> HSET hash_key1  field2 "100"
(integer) 1
127.0.0.1:6379> HSET hash_key1  field3 "100"
(integer) 1
127.0.0.1:6379> HKEYS hash_key1
1) "field1"
2) "field2"
3) "field3"
127.0.0.1:6379> HKEYS hash_key
1) "field2"
2) "field3"
3) "field4"
4) "field5"
5) "field6"
127.0.0.1:6379> 

 

10 . hvals key: 返回 hash 的所有 value。

127.0.0.1:6379> HVALS hash_key
1) "world"
2) "test1"
3) "test2"
4) "test3"
5) "111"
127.0.0.1:6379> HVALS hash_key1
1) "100"
2) "100"
3) "100"
127.0.0.1:6379> 

 

11. hgetall 返回 hash 的所有 filed 和 value

127.0.0.1:6379> HGETALL hash_key
 1) "field2"
 2) "world"
 3) "field3"
 4) "test1"
 5) "field4"
 6) "test2"
 7) "field5"
 8) "test3"
 9) "field6"
10) "111"
127.0.0.1:6379> HGETALL hash_key1
1) "field1"
2) "100"
3) "field2"
4) "100"
5) "field3"
6) "100"
127.0.0.1:6379> 

 

12. HINCRBYFLOAT key field increment:  

Increment the specified field of an hash stored at key, and representing a floating point number, by the specified increment. If the field does not exist, it is set to 0before performing the operation. An error is returned if one of the following conditions occur:

  • The field contains a value of the wrong type (not a string).
  • The current field content or the specified increment are not parsable as a double precision floating point number.

The exact behavior of this command is identical to the one of the INCRBYFLOATcommand, please refer to the documentation of INCRBYFLOAT for further information.

 

13. HSTRLEN key field:返回的字符串长度值。如果键或字段不存在,则返回0。低版本的貌似是HLEN命令查看长度.

 

 

14. HSETNX key field value:

Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.

127.0.0.1:6379> HGET hash_key field1
(nil)
127.0.0.1:6379> HGET hash_key field2
"world"
127.0.0.1:6379> HSETNX hash_key field2 "hello"
(integer) 0
127.0.0.1:6379> HSETNX hash_key field1 "hello"
(integer) 1
127.0.0.1:6379> 

 

五, List类型:

list 是一个链表结构,可以理解为一个每个子元素都是 string 类型的双向链表。 主要功能是 push、pop、获取一个范围的所有值等。操作中 key 理解为链表的名字。

1.  lpush key string 在 key 对应 list 的头部添加字符串元素,返回 1 表示成功,0 表示 key 存在且不是 list 类型。

127.0.0.1:6379[3]> LPUSH list "test"
(integer) 4
127.0.0.1:6379[3]> LPUSH list "test_list"
(integer) 5
127.0.0.1:6379[3]> SET string "string"
OK
127.0.0.1:6379[3]> TYPE set
none
127.0.0.1:6379[3]> TYPE string
string
127.0.0.1:6379[3]> sadd set "set"
(integer) 1
127.0.0.1:6379[3]> TYPE set
set
127.0.0.1:6379[3]> TYPE list
list
127.0.0.1:6379[3]> LPUSH set "hello"
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[3]> LPUSH string "hello"
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[3]> LPUSH list "add list"
(integer) 6
127.0.0.1:6379[3]> 

 


2. rpush key string 在 key 对应 list 的尾部添加字符串元素。

127.0.0.1:6379[3]> RPUSH list "add_something"
(integer) 7
127.0.0.1:6379[3]> LPUSH list2 "hello"
(integer) 1
127.0.0.1:6379[3]> LPUSH list2 "world"
(integer) 2
127.0.0.1:6379[3]> TYPE list2
list
127.0.0.1:6379[3]> RPUSH list2 "add_something"
(integer) 3
127.0.0.1:6379[3]> 

 


3. llen key 返回 key 对应 list 的长度,如果 key 不存在返回 0,如果 key 对应类型不是 list返回错误。

127.0.0.1:6379[3]> LLEN list
(integer) 7
127.0.0.1:6379[3]> LLEN list2
(integer) 3
127.0.0.1:6379[3]> LLEN set
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[3]> LLEN string
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379[3]> 

 


4. lrange key start end 返回指定区间内的元素,下标从 0 开始,负值表示从后面计算,-1 表示倒数第一个元素 ,key 不存在返回空列表

127.0.0.1:6379[3]> LRANGE list 0 3
1) "add list"
2) "test_list"
3) "test"
4) "world"
127.0.0.1:6379[3]> LRANGE lis2 0 1
(empty list or set)
127.0.0.1:6379[3]> LRANGE list2 0 1
1) "world"
2) "hello"
127.0.0.1:6379[3]> 

 


5. ltrim key start end 截取 list 指定区间内元素,成功返回 1,key 不存在返回错误。

127.0.0.1:6379[3]> LTRIM list 1 5
OK
127.0.0.1:6379[3]> LTRIM list 1 10
OK
127.0.0.1:6379[3]> LTRIM list2 5 6
OK
127.0.0.1:6379[3]> 

 


6. lset key index value 设置 list 中指定下标的元素值,成功返回 1,key 或者下标不存在返回错误。

127.0.0.1:6379[3]> LSET list 4 "lset test"
(error) ERR index out of range
127.0.0.1:6379[3]> LSET list 3 "lset test"
OK
127.0.0.1:6379[3]> LRANGE list
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379[3]> LRANGE list 0 3
1) "test"
2) "world"
3) "world"
4) "lset test"
127.0.0.1:6379[3]> 

 


7 lrem key count value 从 List 的头部(count 正数)或尾部(count 负数)删除一定数量(count)匹配 value 的元素,返回删除的元素数量。count 为 0 时候删除全部。

127.0.0.1:6379[3]> LRANGE list 0 3
1) "test"
2) "world"
3) "world"
4) "lset test"
127.0.0.1:6379[3]> LRANGE list 0 3
1) "test"
2) "world"
3) "world"
4) "lset test"
127.0.0.1:6379[3]> LREM list 2 world
(integer) 2
127.0.0.1:6379[3]> LRANGE list 0 3
1) "test"
2) "lset test"
127.0.0.1:6379[3]> 

 


8 lpop key 从 list 的头部删除并返回删除元素。如果 key 对应 list 不存在或者是空返回 nil,如果 key 对应值不是 list 返回错误。

127.0.0.1:6379[3]> LPOP list 
"world"
127.0.0.1:6379[3]> LRANGE list 0 9
1) "hello"
2) "hello"
3) "world"
4) "test"
5) "lset test"
127.0.0.1:6379[3]> LPOP list 
"hello"
127.0.0.1:6379[3]> LRANGE list 0 9
1) "hello"
2) "world"
3) "test"
4) "lset test"
127.0.0.1:6379[3]> LPOP list 
"hello"
127.0.0.1:6379[3]> LRANGE list 0 9
1) "world"
2) "test"
3) "lset test"
127.0.0.1:6379[3]> 

 


9. rpop key 从 list 的尾部删除并返回删除元素。

127.0.0.1:6379[3]> LRANGE list 0 9
1) "world"
2) "test"
3) "lset test"
127.0.0.1:6379[3]> RPOP list
"lset test"
127.0.0.1:6379[3]> LRANGE list 0 9
1) "world"
2) "test"
127.0.0.1:6379[3]> RPOP list
"test"
127.0.0.1:6379[3]> LRANGE list 0 9
1) "world"
127.0.0.1:6379[3]> 

 


10. blpop key1 ...... keyN timeout 从左到右扫描,返回对第一个非空 list 进行 lpop 操作并返回,比如 blpop list1 list2 list3 0 ,如果 list 不存在 list2,list3 都是非空则对 list2 做
lpop 并返回从 list2 中删除的元素。如果所有的 list 都是空或不存在,则会阻塞 timeout
秒,timeout 为 0 表示一直阻塞。当阻塞时,如果有 client 对 key1...keyN 中的任意 key
进行 push 操作,则第一在这个 key 上被阻塞的 client 会立即返回。如果超时发生,则返回
nil。有点像 unix 的 select 或者 poll。

127.0.0.1:6379[3]> BLPOP list list2 100
1) "list"
2) "sex"
127.0.0.1:6379[3]> BLPOP list list2 100
1) "list"
2) "world"
127.0.0.1:6379[3]> 

 

11. brpop 同 blpop,一个是从头部删除一个是从尾部删除。

127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "one"
127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "two"
127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "three"
127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "four"
127.0.0.1:6379[3]> LRANGE list2 0 9
1) "eleven"
2) "ten"
3) "nine"
4) "eight"
5) "seven"
6) "six"
7) "five"
127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "five"
127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "six"
127.0.0.1:6379[3]> BRPOP list2 list 100
1) "list2"
2) "seven"
127.0.0.1:6379[3]> LRANGE list2 0 9
1) "eleven"
2) "ten"
3) "nine"
4) "eight"
127.0.0.1:6379[3]> 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM