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