DEL:
DEL key [key ...]
刪除給定的一個(gè)或多個(gè) key 。
不存在的 key 會(huì)被忽略。
返回值:被刪除 key 的數(shù)量。
# 刪除單個(gè) key
redis> SET name huangz
OK
redis> DEL name
(integer) 1
# 刪除一個(gè)不存在的 key
redis> EXISTS phone
(integer) 0
redis> DEL phone # 失敗,沒有 key 被刪除
(integer) 0
# 同時(shí)刪除多個(gè) key
redis> SET name "redis"
OK
redis> SET type "key-value store"
OK
redis> SET website "redis.com"
OK
redis> DEL name type website
(integer) 3
EXISTS:
EXISTS key
返回值:若 key 存在,返回 1 ,否則返回 0 。
redis> SET db "redis"
OK
redis> EXISTS db
(integer) 1
redis> DEL db
(integer) 1
redis> EXISTS db
(integer) 0
expire:
EXPIRE key seconds
相關(guān)命令expireat(接收參數(shù)unix的時(shí)間戳)
相關(guān)命令pexpire(設(shè)置時(shí)間為毫秒為單位)
相關(guān)命令pexpireat(接收以毫秒為單位的unix的時(shí)間戳)
為給定 key 設(shè)置生存時(shí)間,當(dāng) key 過(guò)期時(shí)(生存時(shí)間為 0 ),它會(huì)被自動(dòng)刪除。
在 Redis 中,帶有生存時(shí)間的key 被稱為『易失的』(volatile)。
生存時(shí)間可以通過(guò)使用 DEL 命令來(lái)刪除整個(gè) key 來(lái)移除,或者被 SET 和 GETSET 命令覆寫(overwrite),這意味著,如果一個(gè)命令只是修改(alter)一個(gè)帶生存時(shí)間的 key的值而不是用一個(gè)新的 key 值來(lái)代替(replace)它的話,那么生存時(shí)間不會(huì)被改變。
比如說(shuō),對(duì)一個(gè)key 執(zhí)行 INCR 命令,對(duì)一個(gè)列表進(jìn)行 LPUSH 命令,或者對(duì)一個(gè)哈希表執(zhí)行 HSET 命令,這類操作都不會(huì)修改key 本身的生存時(shí)間。
另一方面,如果使用 RENAME 對(duì)一個(gè) key進(jìn)行改名,那么改名后的 key 的生存時(shí)間和改名前一樣。
RENAME 命令的另一種可能是,嘗試將一個(gè)帶生存時(shí)間的 key 改名成另一個(gè)帶生存時(shí)間的another_key ,這時(shí)舊的 another_key (以及它的生存時(shí)間)會(huì)被刪除,然后舊的 key 會(huì)改名為 another_key ,因此,新的 another_key的生存時(shí)間也和原本的 key 一樣。
使用 PERSIST 命令可以在不刪除 key 的情況下,移除 key的生存時(shí)間,讓 key 重新成為一個(gè)『持久的』(persistent) key 。
更新生存時(shí)間
可以對(duì)一個(gè)已經(jīng)帶有生存時(shí)間的 key 執(zhí)行 EXPIRE 命令,新指定的生存時(shí)間會(huì)取代舊的生存時(shí)間。
返回值:
設(shè)置成功返回 1 。
當(dāng) key 不存在或者不能為 key 設(shè)置生存時(shí)間時(shí)(比如在低于 2.1.3 版本的 Redis 中你嘗試更新 key 的生存時(shí)間),返回 0 。
redis> SET cache_page "www.google.com"
OK
redis> EXPIRE cache_page 30 # 設(shè)置過(guò)期時(shí)間為 30 秒
(integer) 1
redis> TTL cache_page # 查看剩余生存時(shí)間
(integer) 23
redis> EXPIRE cache_page 30000 # 更新過(guò)期時(shí)間
(integer) 1
redis> TTL cache_page
(integer) 29996
keys
KEYS pattern
查找所有符合給定模式 pattern 的 key 。
KEYS * 匹配數(shù)據(jù)庫(kù)中所有 key 。
KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
KEYS h*llo 匹配 hllo 和 heeeeello 等。
KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
特殊符號(hào)用 \ 隔開
返回值:
符合給定模式的 key 列表。
redis> MSET one 1 two 2 three 3 four 4 # 一次設(shè)置 4 個(gè) key
OK
redis> KEYS *o*
1) "four"
2) "two"
3) "one"
redis> KEYS t??
1) "two"
redis> KEYS t[w]*
1) "two"
redis> KEYS * # 匹配數(shù)據(jù)庫(kù)內(nèi)所有 key
1) "four"
2) "three"
3) "two"
4) "one"
PERSIST
PERSIST key
移除給定 key 的生存時(shí)間,將這個(gè) key 從『易失的』(帶生存時(shí)間 key )轉(zhuǎn)換成『持久的』(一個(gè)不帶生存時(shí)間、永不過(guò)期的 key )。
返回值:
當(dāng)生存時(shí)間移除成功時(shí),返回 1 .
如果 key 不存在或 key 沒有設(shè)置生存時(shí)間,返回 0 。
redis> SET mykey "Hello"
OK
redis> EXPIRE mykey 10 # 為 key 設(shè)置生存時(shí)間
(integer) 1
redis> TTL mykey
(integer) 10
redis> PERSIST mykey # 移除 key 的生存時(shí)間
(integer) 1
redis> TTL mykey
(integer) -1
ttl
TTL key
相關(guān)命令pttl(以毫秒為單位返回key的剩余時(shí)間)
以秒為單位,返回給定 key 的剩余生存時(shí)間
返回值:
當(dāng) key 不存在時(shí),返回 -2 。
當(dāng) key 存在但沒有設(shè)置剩余生存時(shí)間時(shí),返回 -1 。
否則,以秒為單位,返回 key 的剩余生存時(shí)間。
# 不存在的 key
redis> FLUSHDB
OK
redis> TTL key
(integer) -2
# key 存在,但沒有設(shè)置剩余生存時(shí)間
redis> SET key value
OK
redis> TTL key
(integer) -1
# 有剩余生存時(shí)間的 key
redis> EXPIRE key 10086
(integer) 1
redis> TTL key
(integer) 10084
randomkey
RANDOMKEY
從當(dāng)前數(shù)據(jù)庫(kù)中隨機(jī)返回(不刪除)一個(gè) key 。
返回值:
當(dāng)數(shù)據(jù)庫(kù)不為空時(shí),返回一個(gè) key 。
當(dāng)數(shù)據(jù)庫(kù)為空時(shí),返回 nil 。
# 數(shù)據(jù)庫(kù)不為空
redis> MSET fruit "apple" drink "beer" food "cookies" # 設(shè)置多個(gè) key
OK
redis> RANDOMKEY
"fruit"
redis> RANDOMKEY
"food"
redis> KEYS * # 查看數(shù)據(jù)庫(kù)內(nèi)所有key,證明 RANDOMKEY 并不刪除 key
1) "food"
2) "drink"
3) "fruit"
# 數(shù)據(jù)庫(kù)為空
redis> FLUSHDB # 刪除當(dāng)前數(shù)據(jù)庫(kù)所有 key
OK
redis> RANDOMKEY
(nil)
rename
RENAME key newkey
相關(guān)命令renamenx(當(dāng)且僅當(dāng) newkey 不存在時(shí),將 key 改名為 newkey)
將 key 改名為 newkey 。
當(dāng) newkey 相同,或者 key 不存在時(shí),返回一個(gè)錯(cuò)誤。
當(dāng) newkey 已經(jīng)存在時(shí), RENAME 命令將覆蓋舊值。
返回值:
改名成功時(shí)提示 OK ,失敗時(shí)候返回一個(gè)錯(cuò)誤。
# key 存在且 newkey 不存在
redis> SET message "hello world"
OK
redis> RENAME message greeting
OK
redis> EXISTS message # message 不復(fù)存在
(integer) 0
redis> EXISTS greeting # greeting 取而代之
(integer) 1
# 當(dāng) key 不存在時(shí),返回錯(cuò)誤
redis> RENAME fake_key never_exists
(error) ERR no such key
# newkey 已存在時(shí), RENAME 會(huì)覆蓋舊 newkey
redis> SET pc "lenovo"
OK
redis> SET personal_computer "dell"
OK
redis> RENAME pc personal_computer
OK
redis> GET pc
(nil)
redis:1> GET personal_computer # 原來(lái)的值 dell 被覆蓋了
"lenovo"