1、键值通用操作
程序员文章站
2022-07-13 22:12:33
...
1、键值通用操作
-
keys pattern:查找当前数据库中所有匹配pattern的键
- pattern支持*,?,[]匹配
- *:匹配任意(所有)选项
- ?:任意一位
- []:匹配其中包含的任意一项
127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "zs" 4) "a" 5) "w" 6) "k" 7) "ss" 127.0.0.1:6379> keys ? 1) "a" 2) "w" 3) "k" 127.0.0.1:6379> keys [wak] 1) "a" 2) "w" 3) "k"
- pattern支持*,?,[]匹配
-
randomkey:抽取随机键
127.0.0.1:6379> randomkey "key1" 127.0.0.1:6379> randomkey "k" 127.0.0.1:6379> randomkey "zs"
-
exists key:判断是否存在键key,返回1 或 0
127.0.0.1:6379> exists k (integer) 1 127.0.0.1:6379> exists pp (integer) 0
-
type key:判断键的类型
127.0.0.1:6379> type k list
-
del key
127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "zs" 4) "a" 5) "w" 6) "k" 7) "ss" 127.0.0.1:6379> del ss (integer) 1 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "zs" 4) "a" 5) "w" 6) "k"
-
rename key newname
# 正常情况下 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "zs" 4) "a" 5) "w" <--- 6) "k" 127.0.0.1:6379> rename w wa OK 127.0.0.1:6379> keys * 1) "key1" 2) "wa" <---- 3) "mykey" 4) "zs" 5) "a" 6) "k"
# 如果重命名与已有键名重回 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "a" < -- hash 4) "zs" 5) "k" 127.0.0.1:6379> type a hash 127.0.0.1:6379> set aa "new" <-- string OK 127.0.0.1:6379> rename a aa OK 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "aa" <-- hash 4) "zs" 5) "k" 127.0.0.1:6379> type aa hash
- 重命名(new)与已有键(old)名重合,会覆盖重合的键(old),相当于将键(old)的类型改写为重命名键(new)的类型
-
renamenx key
- 与rename一样,但当newkey存在时,不进行操作
127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "aa" 4) "zs" 5) "k" 127.0.0.1:6379> renamenx a aa (integer) 0 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "a" <--没有改变 4) "aa" 5) "zs" 6) "k"
-
select 数据库编号(默认使用0号数据库)
-
flushdb:清空数据
-
move key 数据库编号
#默认0号数据库 127.0.0.1:6379> set q 123 OK 127.0.0.1:6379> move q 1 (integer) 1 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "a" 4) "aa" 5) "zs" 6) "k" #没有q 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> keys * 1) "q"
-
ttl key:查询有效期
127.0.0.1:6379> ttl a
(integer) -1 -->永久有效
127.0.0.1:6379> ttl q
(integer) -2 -->不存在
- expire key seconds:设置生命周期
127.0.0.1:6379> expire o 123 (integer) 1 127.0.0.1:6379> ttl o (integer) 119
- pexpire key ms:设置有效期,单位为毫秒
- persist key:设置永久有效
127.0.0.1:6379> ttl o (integer) 33 127.0.0.1:6379> persist o (integer) 1 127.0.0.1:6379> ttl o (integer) -1
上一篇: ETCD简介与使用
下一篇: 闲谈etcd(三)etcd的使用