【附node操做實例】redis簡明入門系列—字符串類型

下面是字符串類型的相關命令node

賦值、取值

賦值採用如下命令git

SET key hello

若是客戶端返回爲OK則證實寫入成功github

取值採用如下命令redis

GET key // 返回 "hello"

GET key2 // 返回 (nil)

若是key值存在,則返回存儲的鍵值hellocode

若是key不存在,redis會返回nil對象

遞增數字

INCR num

當要操做的鍵不存在時默認鍵值爲0,因此運行該指令後結果爲1,當鍵值不爲整數時,redis會提示錯誤字符串

INCR key

// 會返回如下錯誤
(error) ERR value is not an integer or out of range
注意:
建議使用 incr進行自增,而不是使用 set來執行 +1操做,由於 incr操做是原子性的。若是同時有兩個客戶端操做,最終值只會 +1

增長指定整數

INCRBY num 10

返回num增長10後的值
若是增長鍵的值爲字符串,則報錯以下:(error) ERR value is not an integer or out of rangeget

遞減

DECR num
DECRBY num 10

DECR 命令遞減1string

DECRBY 命令遞減指定數值it

若是鍵的值爲字符串,則報錯以下:(error) ERR value is not an integer or out of range

增長指定浮點數

INCRBYFLOAT num 1.1

以上命令爲鍵爲num的值增長1.1

若是鍵的值爲字符串,則報錯以下:(error) ERR value is not an integer or out of range

向尾部追加值

APPEND key " world!"

返回值爲字符串的總長度,此時key的值爲hello world!

獲取字符串長度

STRLEN key

同時得到/設置多個鍵值

MGET key key2 key3
MSET key value1 key2 value2 key3 value3

redis 鍵命名

鍵的命名通常格式爲

對象類型:對象id:對象屬性

若是多個單次則使用.分開

如:存儲id爲1的好友列表,命名以下:user:1:friends

Node示例

關於字符串的node示例,請跳轉至github查看
https://github.com/Crazycheng...

相關文章
相關標籤/搜索