Redis 系統管理
適用於全體類型的常用指令
- exists key [key …] 判斷key是否存在,存在返回1,不存在返回0
- del key [key …] 刪除key,刪除成功返回1,刪除失敗返回0(不存在)
- type key 返回key元素的數據類型(none:不存在,string:字符,list:列表,set:元組,zset:有序集合,hash:哈希),key 不存在返回空。
- keys pattern 返回一個匹配到的key列表。
- randomkey 隨機獲得一個已經存在的 key,如果當前數據庫爲空,則返回空字符串。
- rename key newkey 更改 key 的名字,新鍵如果存在將被覆蓋。
- renamenx key newkey 更改 key 的名字,新鍵如果存在則更新失敗。
- dbsize 返回當前數據庫的 key 的總數。
Redis 時間相關命令
限定key的生存時間
expire:設置某個 key 的過期時間(秒),比如:expire hello 10 表示設置 hello 這個 key 10 秒後系統自動刪除,注意:如果在還沒有過期的時候,對值進行了改變,那麼那個值會被清除。
- expire key seconds
127.0.0.1:6379> set hello hello
OK
127.0.0.1:6379> expire hello 10
(integer) 1
127.0.0.1:6379> get hello
"hello"
127.0.0.1:6379> get hello //十秒之後
(nil)
查詢 key 剩餘生存時間
ttl:查找某個 key 還有多長時間過期,返回時間單位爲秒。
- ttl key
127.0.0.1:6379> set hello helloword ex 25
OK
127.0.0.1:6379> ttl hello
(integer) 18
127.0.0.1:6379> ttl hello
(integer) 10
127.0.0.1:6379> ttl hello
(integer) -2
127.0.0.1:6379> get hello
(nil)
清除key
- flushdb:清空當前數據庫中的所有鍵。
- flushall:清空所有數據庫中的所有鍵
Redis 設置相關命令
config get:用來讀取運行 Redis 服務器的配置參數。
config set:用於更改運行 Redis 服務器的配置參數。
auth:認證密碼。
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
127.0.0.1:6379> config set requirepass 654321
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "654321"
127.0.0.1:6379> auth 123456
(error) ERR invalid password
127.0.0.1:6379> auth 654321
OK
說明:config get requirepass 可能因爲認證密碼而獲取失敗,通過 auth password,即可獲取到密碼。
- config get max--entries* 查詢數據類型的最大條目:
127.0.0.1:6379> config get *max-*-entries
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> config get *max-*-entries
1) "hash-max-ziplist-entries"
2) "512"
3) "set-max-intset-entries"
4) "512"
5) "zset-max-ziplist-entries"
6) "128"
config resetstat:重置數據統計報告,通常返回值爲“OK”。
127.0.0.1:6379> config resetstat
OK
查詢信息
info [section] 查詢 Redis 相關信息。
- server:Redis server 的常規信息
- clients:Client 的連接選項
- memory:存儲佔用相關信息
- persistence:RDB and AOF 相關信息
- stats:常規統計信息
- replication:Master/Slave 請求信息
- cpu:CPU 佔用信息統計
- cluster:Redis集羣信息
- keyspace:數據庫信息統計
- all:返回所有信息
- default:返回常規設置信息