1. 程式人生 > >Redis字串型別

Redis字串型別

字串是Redis中最基本的資料型別,他能儲存任何形式的字串,包括二進位制資料。

命令

  1. 賦值

    SET key value
    
    > SET key hello
    OK
  2. 取值

    GET key
    
    > GET key
    "hello"
  3. 遞增數字

    INCR key
    
    > INCR num
    (integer) 1
    

    要操作的鍵不存在時預設鍵值為0,不是整數時會報錯

  4. 遞減數字

    DECR key
    
    > DECR num
    (integer) 0
    

    要操作的鍵不存在時預設鍵值為0,不是整數時會報錯

  5. 增加制定的整數

    INCRBY key increment
    
    > INCRBY num 2
    (integer) 2
  6. 減少指定的整數

    DECRBY key increment
    
    > INCRBY num 2
    (integer) 0
  7. 增加制定浮點數

    INCRBYFLOAT key increment
    
    > INCRBYFLOAT num 2.7
    "2.7"
  8. 向尾部追加值

    APPEND key increment
    
    > SET key hello
    OK
    > APPEND key " world!"
    (integer) 12

    返回值是追加後字串的總長度

  9. 獲取字串長度

    STRLEN key
    
    > STRLEN key
    (integer) 12

    如果鍵不存在則返回0

  10. 同時設定多個鍵值

    MSET key value [key value ...]
    
    > MSET key1 v1 key2 v2 key3 v3
    OK
  11. 同時獲取多個鍵值

    MGET key value [key value ...]
    
    > MSET key1 key3
    1) "v1"
    2) "v3"
  12. 位獲取

    GETBIT key offset
    
    > SET foo bar
    OK
    > GETBIT foo 0
    (integer) 0
    > GETBIT foo 6
    (integer) 1 

    如果獲取的二進位制位的索引超出了鍵值的二進位制位的實際長度則預設位值是0

  13. 位設定

    SETBIT key offset value
    
    > SETBIT key 6 0
    (integer) 1
    > SETBIT key 7 1
    (integer) 0
    > GET foo
    "aar"

    如果設定的位置超過了鍵值的二進位制位的長度,SETBIT會自動將總監的二進位制位設為0。同理設定一個不存在的鍵的指定二進位制位的值會自動將其前面的位賦值為0。

  14. 位統計

    BITCOUNT key [start] [end]
    
    > BITCOUNT foo
    (integer) 10
    > BITCOUNT foo 0 1
    (integer) 6
  15. 位運算

    BITOP operation destkey key [key ...]
    
    > SET foo1 bar
    OK
    > SET foo2 aar
    OK
    > BITOP OR res foo1 foo2
    "Car"
  16. 獲得指定鍵的第一個位值是0或1的位置

    BITPOS destkey value [start] [end]
    
    > SET foo bar
    OK
    > BITPOS foo 1
    (integer) 1
    > BITPOS foo 1 1 2
    (integer) 9