标签:png dom 关系型数据库 限时 image 随机 缓存 int 存储
1、常用命令
(1)keys *:返回满足的所有键,可以模糊匹配
127.0.0.1:6379> keys *
1) "123"
2) "zhaihb"
3) "zhai"
127.0.0.1:6379> keys zha*
1) "zhaihb"
2) "zhai"
(2)exists key:是否存在指定的key,存在返回1,不存在返回0
127.0.0.1:6379> exists 666
(integer) 0
127.0.0.1:6379> exists zhai
(integer) 1
(3)expire key 过期时间(s):设置某一个key的过期时间,可以减少内存的占用
127.0.0.1:6379> ttl zhai
(integer) -1
127.0.0.1:6379> expire zhai 20
(integer) 1
127.0.0.1:6379> ttl zhai
(integer) 16
127.0.0.1:6379> ttl zhai
(integer) 12
127.0.0.1:6379> ttl zhai
(integer) 5
127.0.0.1:6379> ttl zhai
(integer) -2
应用场景:限时的优惠、网站的数据缓存、手机验证码、限制网站访客的访问频率(防流量攻击)
(4)del key:删除某一个key
127.0.0.1:6379> keys *
1) "123"
2) "zhaihb"
127.0.0.1:6379> del 123
(integer) 1
127.0.0.1:6379> keys *
1) "zhaihb"
(5)ttl key:查看剩余时间,当key不存在的时候返回-2,存在但是没有设置剩余生存时间(永久有效)返回-1,否则返回剩余时间(以秒为单位)
127.0.0.1:6379> ttl zhai
(integer) -1
127.0.0.1:6379> expire zhai 20
(integer) 1
127.0.0.1:6379> ttl zhai
(integer) 16
127.0.0.1:6379> ttl zhai
(integer) 12
127.0.0.1:6379> ttl zhai
(integer) 5
127.0.0.1:6379> ttl zhai
(integer) -2
(6)persist key:取消过期时间
127.0.0.1:6379> keys *
1) "zhaihb"
127.0.0.1:6379> expire zhaihb 30
(integer) 1
127.0.0.1:6379> ttl zhaihb
(integer) 26
127.0.0.1:6379> ttl zhaihb
(integer) 19
127.0.0.1:6379> persist zhaihb
(integer) 1
127.0.0.1:6379> ttl zhaihb
(integer) -1
(7)pexpire key 时间(ms):修改key的过期时间
127.0.0.1:6379> keys *
1) "zhaihb"
127.0.0.1:6379> ttl zhaihb
(integer) -1
127.0.0.1:6379> pexpire zhaihb 30000
(integer) 1
127.0.0.1:6379> ttl zhaihb
(integer) 26
127.0.0.1:6379> ttl zhaihb
(integer) 20
127.0.0.1:6379> pexpire zhaihb 30000
(integer) 1
127.0.0.1:6379> ttl zhaihb
(integer) 25
127.0.0.1:6379> pexpire zhaihb 60000
(integer) 1
127.0.0.1:6379> ttl zhaihb
(integer) 55
(8)randomkey:随机返回一个key
127.0.0.1:6379> keys *
1) "zhang"
2) "huang"
3) "liu"
4) "zhao"
5) "zhai"
127.0.0.1:6379> randomkey
"zhang"
127.0.0.1:6379> randomkey
"zhang"
127.0.0.1:6379> randomkey
"zhang"
127.0.0.1:6379> randomkey
"zhai"
127.0.0.1:6379> randomkey
"zhao"
127.0.0.1:6379> randomkey
"zhang"
127.0.0.1:6379> randomkey
"zhang"
127.0.0.1:6379> randomkey
"huang"
127.0.0.1:6379> randomkey
"zhai"
127.0.0.1:6379> randomkey
"liu"
127.0.0.1:6379> randomkey
"zhang"
127.0.0.1:6379> randomkey
"zhao"
127.0.0.1:6379> randomkey
"zhao"
(9)rename key 新的key名字:重命名key
命令的方式:
127.0.0.1:6379> keys *
1) "zhang"
2) "zhaihb"
3) "huang"
4) "liu"
5) "zhao"
127.0.0.1:6379> rename zhaihb zhai
OK
127.0.0.1:6379> keys *
1) "zhang"
2) "huang"
3) "liu"
4) "zhao"
5) "zhai"
运用工具重命名key:
选中要修改的key,直接在文本框中修改即可:
利用命令查看:
127.0.0.1:6379> keys *
1) "zhang"
2) "liu"
3) "zhao"
4) "huangzzz"
5) "zhai"
在点击rename按钮后,在下面的控制台打印如下信息:
[07:26:00] test > [runCommand] RENAME huang huangzzz -> response recieved
(10)pttl key:以毫秒为单位返回过期时间
127.0.0.1:6379> expire liu 30
(integer) 1
127.0.0.1:6379> pttl liu
(integer) 22015
127.0.0.1:6379> pttl liu
(integer) 11054
127.0.0.1:6379> pttl liu
(integer) 6600
127.0.0.1:6379> pttl liu
(integer) -2
(11)type key:返回key所存储的值的类型
127.0.0.1:6379> get zhao
"202004"
127.0.0.1:6379> type zhao
string
2、key的命名规范
(1)单个key允许存入512M大小的数据,key不能太长,尽量不要超过1024字节,不仅会消耗内存还会降低查找的效率
(2)key也不能太短,要保证key的可读性,因为数据的关联不如关系型数据库,因此,在命名的时候要保证相关联的数据能够区别出来
(3)key的名称区分大小写
127.0.0.1:6379> keys *
1) "zhang"
2) "zhao"
3) "huangzzz"
4) "zhai"
127.0.0.1:6379> set ZHAI 123
OK
127.0.0.1:6379> keys *
1) "zhang"
2) "zhao"
3) "ZHAI"
4) "huangzzz"
5) "zhai"
标签:png dom 关系型数据库 限时 image 随机 缓存 int 存储
原文地址:https://www.cnblogs.com/zhai1997/p/13205964.html