Redis 笔记(02)— keys 键相关命令(查询数据库key数量、判断key是否存在、指定key过期时间、查看key类型、查看key剩余秒数、选择数据库、删除key、删除数据库)
生活随笔
收集整理的這篇文章主要介紹了
Redis 笔记(02)— keys 键相关命令(查询数据库key数量、判断key是否存在、指定key过期时间、查看key类型、查看key剩余秒数、选择数据库、删除key、删除数据库)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. keys 鍵相關(guān)命令
| 命令 | 說明 |
|---|---|
| dbsize | 返回當(dāng)前數(shù)據(jù)路的key數(shù)量 |
| exists key | 測(cè)試指定key是否存在 |
| expire key seconds | 為key指定過期時(shí)間 |
| type key | 返回指定key的value類型 |
| keys pattern | 返回指定模式的所有key |
| rename oldkey newkey | 重命名key |
| ttl key | 返回key的過期剩余秒數(shù) |
| select db-index | 選擇數(shù)據(jù)庫 |
| move key db-index | 把key從當(dāng)前數(shù)據(jù)庫移動(dòng)到指定數(shù)據(jù)庫 |
| flushdb | 刪除當(dāng)前數(shù)據(jù)庫所有key |
| flushall | 刪除所有數(shù)據(jù)庫中的所有key |
| del key1 key2….keyN | 刪除指定key |
2. 使用示例
127.0.0.1:6379> dbsize
(integer) 4
127.0.0.1:6379> keys *
1) "key"
2) "c"
3) "score_rank"
4) "a"
127.0.0.1:6379> exists a
(integer) 1
127.0.0.1:6379> exists b
(integer) 0
127.0.0.1:6379> exists c
(integer) 1
127.0.0.1:6379> type a
string
127.0.0.1:6379> type score_rank
zset
127.0.0.1:6379>
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty array)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> type a
string
127.0.0.1:6379> get a
"hello"
127.0.0.1:6379> move a 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "a"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379>
expire 設(shè)置成功返回 1 。 當(dāng) key 不存在或者不能為 key 設(shè)置過期時(shí)間時(shí)返回 0 。
127.0.0.1:6379> expire x 10
(integer) 0
127.0.0.1:6379> set x "192.168.0.1"
OK
127.0.0.1:6379> expire x 10
(integer) 1
127.0.0.1:6379> ttl x
(integer) 6
127.0.0.1:6379> ttl x
(integer) 5
127.0.0.1:6379>
TTL 命令當(dāng) key 不存在時(shí),返回 -2 。 當(dāng) key 存在但沒有設(shè)置剩余生存時(shí)間時(shí),返回 -1 。 否則,以秒為單位,返回 key 的剩余生存時(shí)間。
127.0.0.1:6379> ttl AAA
(integer) -2
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "key"
2) "x"
3) "c"
4) "score_rank"
127.0.0.1:6379> del key
(integer) 1
127.0.0.1:6379> keys *
1) "x"
2) "c"
3) "score_rank"
127.0.0.1:6379>
127.0.0.1:6379[1]> keys *
1) "a"
127.0.0.1:6379[1]> flushdb
OK
127.0.0.1:6379[1]> keys *
(empty array)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
1) "c"
2) "score_rank"
127.0.0.1:6379> flushall
OK
127.0.0.1:6379>
總結(jié)
以上是生活随笔為你收集整理的Redis 笔记(02)— keys 键相关命令(查询数据库key数量、判断key是否存在、指定key过期时间、查看key类型、查看key剩余秒数、选择数据库、删除key、删除数据库)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis 笔记(01)— 安装、启动配
- 下一篇: Redis 笔记(03)— string