Redis字串型別
阿新 • • 發佈:2018-11-23
字串是Redis中最基本的資料型別,他能儲存任何形式的字串,包括二進位制資料。
命令
賦值
SET key value > SET key hello OK
取值
GET key > GET key "hello"
遞增數字
INCR key > INCR num (integer) 1
要操作的鍵不存在時預設鍵值為0,不是整數時會報錯
遞減數字
DECR key > DECR num (integer) 0
要操作的鍵不存在時預設鍵值為0,不是整數時會報錯
增加制定的整數
INCRBY key increment > INCRBY num 2 (integer) 2
減少指定的整數
DECRBY key increment > INCRBY num 2 (integer) 0
增加制定浮點數
INCRBYFLOAT key increment > INCRBYFLOAT num 2.7 "2.7"
向尾部追加值
APPEND key increment > SET key hello OK > APPEND key " world!" (integer) 12
返回值是追加後字串的總長度
獲取字串長度
STRLEN key > STRLEN key (integer) 12
如果鍵不存在則返回0
同時設定多個鍵值
MSET key value [key value ...] > MSET key1 v1 key2 v2 key3 v3 OK
同時獲取多個鍵值
MGET key value [key value ...] > MSET key1 key3 1) "v1" 2) "v3"
位獲取
GETBIT key offset > SET foo bar OK > GETBIT foo 0 (integer) 0 > GETBIT foo 6 (integer) 1
如果獲取的二進位制位的索引超出了鍵值的二進位制位的實際長度則預設位值是0
位設定
SETBIT key offset value > SETBIT key 6 0 (integer) 1 > SETBIT key 7 1 (integer) 0 > GET foo "aar"
如果設定的位置超過了鍵值的二進位制位的長度,SETBIT會自動將總監的二進位制位設為0。同理設定一個不存在的鍵的指定二進位制位的值會自動將其前面的位賦值為0。
位統計
BITCOUNT key [start] [end] > BITCOUNT foo (integer) 10 > BITCOUNT foo 0 1 (integer) 6
位運算
BITOP operation destkey key [key ...] > SET foo1 bar OK > SET foo2 aar OK > BITOP OR res foo1 foo2 "Car"
獲得指定鍵的第一個位值是0或1的位置
BITPOS destkey value [start] [end] > SET foo bar OK > BITPOS foo 1 (integer) 1 > BITPOS foo 1 1 2 (integer) 9