Redis基本命令
- SET key data #存数据
- GET data #取数据
- KEYS * #查看所有数据
- DEL key #在key存在时删除key
- EXISTS key #检查key是否存在
- EXPIRE key seconds #为给定key设置过期时间(以秒为单位)
- PEXPIRE key milliseconds #设置key的过期时间,以毫秒为单位
- TTL key #以秒为单位,返回给定key的剩余生存时间
- PTTL key #以毫秒为单位返回key的剩余过期时间
- PERSIST key #移除key的过期时间,key将持久保持(过期的元素无法回复)
- keys pattern #查找所有符合给定模式(pattern)的key。keys 通配符:*代表所有 ?代表一个字符
- RANDOMKEY #从当前数据库中随机返回一个key
- RANAME key newname #修改key的名称
- MOVE key db #将当前数据库的key移动到给定的数据库db当中
- TYPE key 返回key所存储的值的类型
Redis命令应用场景
EXPIRE key second
1.限时的优惠活动信息
2.网站数据缓存(对于一些需要定时更新的数据)
3.手机验证码
4.限制网站的方可访问频率(例如:1分钟最多访问10次)
5.命令不区分大小写,但是key区分大小写
Key的命名建议
redis 单个key 存入512M大小
1.key不要太长,尽量不要超过1024字节,这不仅消耗内存, 而且会降低查询效率
2.key也不要太短,太短的话,key的可读性会降低
3.在一个项目中,key最好使用统一的命名格式,列入user:123:password;
4.key区分大小写