Redis通用命令

心已入冬 提交于 2020-03-25 17:25:59

key通用操作

删除指定key

del key

获取key是否存在

exists key

获取key的类型

type key

为指定key设置有效期

expire key seconds
pexpire key milliseconds
expireat key timestamp
pexpireat key milliseconds-timestamp

获取key的有效时间

ttl key
pttl key 

切换key从时效性转换为永久性

persist key

 查询key 

keys pattern

 查询匹配规则

keys * 查询所有
keys it* 查询所有以it开头
keys *heima 查询所有以heima结尾
keys ??heima 查询所有前面两个字符任意,后面以heima结尾
keys user:? 查询所有以user:开头,最后一个字符任意
keys u[st]er:1 查询所有以u开头,以er:1结尾,中间包含一个字母, s或t

为key改名 

rename key newkey
renamenx key newkey

对所有key排序

sort

其他key通用操作

help @generic

 演示如下:

127.0.0.1:6379> keys *
1) "n1"
2) "name"
3) "aa"
4) "num"
127.0.0.1:6379> EXISTS n1
(integer) 1
127.0.0.1:6379> hset u1 id 123 name wgr
(integer) 2
127.0.0.1:6379> LPush list list1
(integer) 1
127.0.0.1:6379> SADD set1 set1
(integer) 1
127.0.0.1:6379> ZADD zset1 1 zset1
(integer) 1
127.0.0.1:6379> type zset1
zset
127.0.0.1:6379> EXPIRE name 3
(integer) 1
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> EXPIRE n1 50
(integer) 1
127.0.0.1:6379> ttl n1
(integer) 44
127.0.0.1:6379> ttl n1
(integer) 43
127.0.0.1:6379> PTTL n1
(integer) 36247
127.0.0.1:6379> ttl aa
(integer) 20
127.0.0.1:6379> PERSIST aa
(integer) 1
127.0.0.1:6379> ttl aa
(integer) -1
127.0.0.1:6379> keys *
1) "set1"
2) "u1"
3) "list"
4) "aa"
5) "num"
6) "zset1"
127.0.0.1:6379> keys set1
1) "set1"
127.0.0.1:6379> keys *set1
1) "set1"
2) "zset1"
127.0.0.1:6379> keys ?set1
1) "zset1"
127.0.0.1:6379> keys nu?
1) "num"
127.0.0.1:6379> set str str
OK
127.0.0.1:6379> set str1 str1
OK
127.0.0.1:6379> set str2 str2
OK
127.0.0.1:6379> rename str str3
OK
127.0.0.1:6379> keys *
1) "u1"
2) "set1"
3) "list"
4) "str3"
5) "str1"
6) "zset1"
7) "aa"
8) "num"
9) "str2"
127.0.0.1:6379> RENAMENX str1 str2
(integer) 0
127.0.0.1:6379> LPUSH bb 12
(integer) 1
127.0.0.1:6379> LPUSH bb 123
(integer) 2
127.0.0.1:6379> LPUSH bb 124
(integer) 3
127.0.0.1:6379> LPUSH bb 353
(integer) 4
127.0.0.1:6379> sort bb
1) "12"
2) "123"
3) "124"
4) "353"

 db 基本操作 

切换数据库

select index

其他操作

quit
ping
echo message

数据移动

move key db

数据清除

dbsize
flushdb
flushall

演示如下:

127.0.0.1:6379> sort bb
1) "12"
2) "123"
3) "124"
4) "353"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>  ping
PONG
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> move bb 1
(integer) 1
127.0.0.1:6379> sort bb
(empty list or set)
127.0.0.1:6379>
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> sort bb
1) "12"
2) "123"
3) "124"
4) "353"
127.0.0.1:6379[1]> dbsize
(integer) 1
127.0.0.1:6379[1]> sort bb
1) "12"
2) "123"
3) "124"
4) "353"
127.0.0.1:6379[1]> FLUSHDB
OK
127.0.0.1:6379[1]> sort bb
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
1) "u1"
2) "set1"
3) "list"
4) "str3"
5) "str1"
6) "zset1"
7) "aa"
8) "num"
9) "str2"
127.0.0.1:6379>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!