这一篇我们一起来学习下key的基本操作,这些操作虽说在高级语言当中都被做了进一步封装,但是对于运维人员来说还是很有必要掌握的。
我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。
准备条件
redis命令都是在成功连接到redis服务的客户端上执行,这里还是采用前几篇使用的单机演示环境
[root@testmachine bin]# ./redis-cli -a xiaofu
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379>
帮助信息和清屏
帮助信息的查看格式如下
127.0.0.1:6379> help
redis-cli 5.0.7
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6379>
例如要查看get
命令的帮助信息
127.0.0.1:6379> help get
GET key
summary: Get the value of a key
since: 1.0.0
group: string
127.0.0.1:6379>
查看string
这个group的信息,就会显示所有这个group所有命令的信息
127.0.0.1:6379> help @string
如果屏幕上消息太多,清空屏幕可以
127.0.0.1:6379> clear
key命名规范
既然是对key做操作,顺便一提key的命名规范。
非关系型数据库中,不同key之间是没有关联的,所以在key当中用分隔符来表示层级关系,例如class_id_name
,class_id_score
等等。考虑到有些层级的命名本身就是有下划线的,所以业内统一采用冒号:
做为分隔符。
上面的两个key规范点就应该写作class:id:name
和class_id_score
。
key操作集合
下列操作不区分大小写。
对key的完整通用操作可以通过help @generic
来查看
- del key
删除key,可以用空格连接来一次删除多个key。失败返回0,成功返回删除的key的数目
127.0.0.1:6379> keys *
1) "name"
2) "test5"
3) "age"
4) "test3"
5) "test2"
6) "test1"
7) "hobby"
127.0.0.1:6379> del test1 test2 test3
(integer) 3
127.0.0.1:6379>
- exists key
检查key是否存在,可以用空格连接来一次检查多个key。失败返回0,成功返回存在的key的数目
.0.0.1:6379> keys *
1) "name"
2) "test5"
3) "age"
4) "hobby"
127.0.0.1:6379> exists name age sex
(integer) 2
127.0.0.1:6379>
- expire key seconds
设置key的存活时间,过期以后会被删除。默认情况下所有key是永久有效的。
127.0.0.1:6379> set temp flower
OK
127.0.0.1:6379> expire temp 10
(integer) 1
127.0.0.1:6379>
为不同类型的key设置超时时间是一种节约内存的有效手段
- expireat key timestamp
在某时间戳过期,在linux系统非常常见
127.0.0.1:6380> set a 1
OK
127.0.0.1:6380> expireat a 1581870799
(integer) 1
127.0.0.1:6380> ttl a
(integer) 42
127.0.0.1:6380>
- ttl key
查看key剩余存活时间,如果返回-1表示永久有效,返回-2表示key已过期被删除
127.0.0.1:6379> set temp flower
OK
127.0.0.1:6379> expire temp 10
(integer) 1
127.0.0.1:6379> ttl temp
(integer) 6
127.0.0.1:6379> ttl temp
(integer) 2
127.0.0.1:6379> ttl temp
(integer) -2
127.0.0.1:6379> keys *
1) "name"
2) "test5"
3) "age"
4) "hobby"
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379>
- pttl key
和ttl的作用一样,不过是以毫秒为单位去返回存活时间。
- persist key
移除key的过期时间
127.0.0.1:6379> set temp flower
OK
127.0.0.1:6379> expire temp 20
(integer) 1
127.0.0.1:6379> ttl temp
(integer) 17
127.0.0.1:6379> persist temp
(integer) 1
127.0.0.1:6379> ttl temp
(integer) -1
127.0.0.1:6379>
- keys pattern
按照指定格式去查找符合要求的key,其中通配符*
代表任意个任意字符,通配符?
代表任意单个字符,也可以用[]
来匹配多个字符中的一个
127.0.0.1:6379> keys *
1) "temp2"
2) "test"
3) "temp"
4) "test2"
127.0.0.1:6379> keys temp?
1) "temp2"
127.0.0.1:6379> keys temp*
1) "temp2"
2) "temp"
127.0.0.1:6379>
127.0.0.1:6380> keys c[ht]at
1) "chat"
- randomkey
从当前数据库中随意返回一个key。
这里既然提到了当前数据库,顺便提一嘴,redis一共支持0-15一共16个数据库,默认情况下使用的是第0号数据库。通过select n
可以切换到数据库n
,不过需要注意下标是从0开始的。
127.0.0.1:6379> randomkey
"test"
127.0.0.1:6379> randomkey
"test2"
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> keys *
(empty list or set)
127.0.0.1:6379[3]> select 0
OK
127.0.0.1:6379> keys *
1) "temp2"
2) "test"
3) "temp"
4) "test2"
127.0.0.1:6379>
- rename key newkey
给key重命名
127.0.0.1:6379> keys *
1) "temp2"
2) "test"
3) "temp"
4) "test2"
127.0.0.1:6379> rename test2 temp3
OK
127.0.0.1:6379> keys *
1) "temp2"
2) "temp3"
3) "temp"
4) "test"
127.0.0.1:6379>
- move key db
移动一个key到另一个数据库中,注意这里不支持通配符,只能移动单个key
127.0.0.1:6379> keys *
1) "temp2"
2) "temp3"
3) "temp"
4) "test"
127.0.0.1:6379> move test 1
(integer) 1
127.0.0.1:6379> keys *
1) "temp2"
2) "temp3"
3) "temp"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "test"
127.0.0.1:6379[1]>
- type key
返回key对应值的数据类型
总结
最后一个查询数据类型的命令没有详细展开,因为等下一篇我们一起学习了redis中的数据类型就自然了解了。