Redis筆記(四)KEY相關命令
阿新 • • 發佈:2018-12-19
KEYS 查詢所有符合給定模式pattern(正則表示式)的 key 。
KEYS pattern
127.0.0.1:6379> KEYS *
1) "say"
2) "res"
3) "userInfo2"
4) "test7"
5) "test4"
6) "userInfo3"
7) "counter"
8) "test6"
9) "TEST"
10) "foo"
11) "test2"
12) "test5"
13) "test8"
常用的正則表示式
* :匹配任意個字元
? :匹配一個任意字元
[] :匹配[]之間的一個字元
\x:匹配特殊字元
EXISTS 返回key是否存在
EXISTS key [key ...]
127.0.0.1:6379> EXISTS counter
(integer) 1
127.0.0.1:6379> EXISTS counters
(integer) 0
TYPE key
127.0.0.1:6379> TYPE linux string 127.0.0.1:6379> TYPE userInfo //不存在的key返回none none 127.0.0.1:6379> TYPE userInfo1 hash
EXPIRE 設定key的過期時間,超過時間後,將會自動刪除該key。
EXPIRE key seconds1
127.0.0.1:6379> EXPIRE linux 10 //設定過期時間10秒
(integer) 1
127.0.0.1:6379> TTL linux
(integer) 3
127.0.0.1:6379> EXPIRE linux 10
(integer) 1
127.0.0.1:6379> TTL linux
(integer) 8
127.0.0.1:6379> TTL linux
(integer) -2
//如果key已經存在過期時間,在通過EXPIRE設定的時候會覆蓋之前過期。
MOVE 將當前資料庫的 key 移動到給定的資料庫 db 當中。 如果當前資料庫(源資料庫)和給定資料庫(目標資料庫)有相同名字的給定 key ,或者 key 不存在於當前資料庫,那麼 MOVE 沒有任何效果。
MOVE key db
127.0.0.1:6379> SET testMove MOVE
OK
127.0.0.1:6379> GET testMove
"MOVE"
127.0.0.1:6379> MOVE testMove 1
(integer) 1
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> EXISTS testMove
(integer) 1
RENAME 將key重新命名為newkey,如果key與newkey相同,將返回一個錯誤。如果newkey已經存在,則值將被覆蓋。
RENAME key newkey
127.0.0.1:6379> RENAME test8 test88
OK
127.0.0.1:6379> GET test8
(nil)
127.0.0.1:6379> GET test88
"8"