键的命令
- 查找所有的键
keys *
例如:
127.0.0.1:6379> keys *
1) "ht3"
2) "s1"
3) "ht1"
4) "week"
5) "five"
6) "thursday"
7) "two"
8) "tuesday"
9) "t2"
10) "t1"
11) "three"
12) "first"
13) "saturday"
14) "l2"
15) "t3"
16) "l1"
17) "ht2"
18) "s2"
19) "s3"
20) "z1"
21) "seven"
22) "four"
23) "t4"
24) "number"
25) "six"
26) "salary"
- 查找匹配的键,支持正则匹配
keys pattern
例如:查找以s开头的键
127.0.0.1:6379> keys s*
1) "s1"
2) "saturday"
3) "s2"
4) "s3"
5) "seven"
6) "six"
7) "salary"
- 判断一个键是否存在,如果存在返回1,不存在返回0
exists key [key ...]
例如:判断一个键,或多个键是否存在
127.0.0.1:6379> exists s1
(integer) 1
127.0.0.1:6379> exists s
(integer) 0
127.0.0.1:6379> exists s1 s2
(integer) 2
127.0.0.1:6379> exists s1 s
(integer) 1
- 查看key对应的value类型
type key
例如:查看key的value类型
127.0.0.1:6379> type s1
set
127.0.0.1:6379> type l1
list
127.0.0.1:6379> type ht1
hash
127.0.0.1:6379> type str //不存在的键返回none
none
127.0.0.1:6379> type number
string
127.0.0.1:6379> type z1
zset
- 删除键及对应的值
del key
例如:删除键及对应的值
127.0.0.1:6379> del three
(integer) 1
127.0.0.1:6379> del four five
(integer) 2
127.0.0.1:6379> exists three //不存在的键,返回0
(integer) 0
127.0.0.1:6379> del three six //three键不存在,只能删除six键
(integer) 1
127.0.0.1:6379> exists six //six键被删除,返回0
(integer) 0
- 设置过期时间,以秒为单位
- 创建时没有设置过期时间则一直存在,直到使用使用DEL移除
expire key seconds
例如:设置一个键的过期时间
127.0.0.1:6379> expire thursday 60
(integer) 1
127.0.0.1:6379> ttl thursday
(integer) 52
127.0.0.1:6379> ttl thursday
(integer) 49
127.0.0.1:6379> exists thursday
(integer) 1
127.0.0.1:6379> ttl thursday
(integer) 31
127.0.0.1:6379> get thursday
"friday"
127.0.0.1:6379> ttl thursday
(integer) 7
127.0.0.1:6379> exists thursday
(integer) 0