AdSense

網頁

2024/4/29

Redis Strings基本命令操作

Strings是Redis最簡單的資料型態,基本操作如下。


使用SET放入一筆資料,後接第一個參數為key,第二個參數為value。

例如下面SET employee:1 "John"放入一筆key為employee:1,value為字串"John"

redis> SET employee:1 "John"
OK

使用GET取得一筆資料,後接key來取得相應的value。

redis> GET employee:1
"John"

若再次用SET輸入同樣的key但不同value,則新的value會覆蓋原本的value。

redis> SET employee:1 "Mary"
OK
redis> GET employee:1
"Mary"

SET若後接NX參數,則只有key不存在時才會寫入。

redis> SET employee:1 "John" NX
(nil)
redis> GET employee:1
"Mary"

SET後接EX參數,可設定到期時間(秒數),時間到期後會自動刪除。

例如下面設定到期時間為10秒,則再10秒後自動刪除

redis> SET employee:1 "John" EX 10
OK
redis>GET employee:1
"John"

(after 10 seconds...)

redis> GET employee:1
(nil)

SET NX為Redis實現分散式鎖(distributed lock)的加鎖動作。


使用DEL後接要刪除的key名稱可刪除key。

redis> SET employee:1 "John"
OK
redis> DEL employee:1
(integer) 1
redis> GET employee:1
(nil)


沒有留言:

AdSense