天天看點

redis各資料類型基本操作---String操作

String資料類型

bit操作

  • BITFIELDkey [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]:

  • BITCOUNT key [start end]:統計字元串被設定為1的bit數.

  • BITPOS key bit [start] [end]:傳回字元串裡面第一個被設定為1或者0的bit位。

  • BITOP operation destkey key [key ...]:BITOP operation destkey key [key ...]

字串操作

  • APPEND key value:如果 

    key

     已經存在,并且值為字元串,那麼這個指令會把 

    value

     追加到原來值(value)的結尾。 如果 

    key

     不存在,那麼它将首先建立一個空字元串的

    key

    ,再執行追加操作,這種情況 APPEND 将類似于 SET 操作。

     例:

10.32.0.18:6380> exists test1
(integer) 0
10.32.0.18:6380> append test1 abc
(integer) 3
10.32.0.18:6380> get test1
"abc"
10.32.0.18:6380> exists test1
(integer) 1
10.32.0.18:6380> append test1 abcde
(integer) 8
10.32.0.18:6380> get test1
"abcabcde"
           

數值操作

  • BITOP operation destkey key [key ...]:對key對應的數字做減1操作。如果key不存在,那麼在操作之前,這個key對應的值會被置為0。如果key有一個錯誤類型的value或者是一個不能表示成數字的字元串,就傳回錯誤。這個操作最大支援在64位有符号的整型數字。

例:

10.32.0.18:6380> exists decr-test1
(integer) 0
10.32.0.18:6380> decr decr-test1   //不存在的key值,會先被指派0,然後-1
(integer) -1
10.32.0.18:6380> get decr-test1
"-1"
10.32.0.18:6380> set decr-test2 10
OK
10.32.0.18:6380> decr decr-test2
(integer) 9
10.32.0.18:6380> get decr-test2
"9"
10.32.0.18:6380> set decr-str abc   //對字元串操作,會傳回錯誤
OK
10.32.0.18:6380> decr decr-str
(error) ERR value is not an integer or out of range
           
  • 将key對應的數字減decrement。如果key不存在,操作之前,key就會被置為0。如果key的value類型錯誤或者是個不能表示成數字的字元串,就傳回錯誤。這個操作最多支援64位有符号的正型數字。

例:

10.32.0.18:6380> decrby test3 2
(integer) -2
10.32.0.18:6380> get test3
"-2"
10.32.0.18:6380> decrby test3 -10
(integer) 8
10.32.0.18:6380> get test3
"8"
10.32.0.18:6380> set test4 abc
OK
10.32.0.18:6380> decrby test4 10
(error) ERR value is not an integer or out of range
           

繼續閱讀