Home  >  Article  >  Database  >  redis过期策略

redis过期策略

WBOY
WBOYOriginal
2016-06-07 16:13:195119browse

一、redis的key有六种过期策略 1、noeviction:一旦内存满则返回错误 2、allkeys-lru:对所有的key进行LRU 3、volatile-lru:只对设置了过期的key进行LRU(默认的方式) 4、allkeys-random:随机剔除一个key 5、volatile-random:对设置过期的key进行随机剔

一、redis的key有六种过期策略

1、noeviction:一旦内存满则返回错误

2、allkeys-lru:对所有的key进行LRU

3、volatile-lru:只对设置了过期的key进行LRU(默认的方式)

4、allkeys-random:随机剔除一个key

5、volatile-random:对设置过期的key进行随机剔除一个

6、volatile-ttl:删除即将过期的key

根据官网的描述,redis使用的LRU并非真正的LRU算法,精确度并不高。redis3.0版本的LRU会维持一个候选池,性能会更加好。

二、一些内存和过期策略的设置和查看

1、设置实例的内存大小(本地执行可以不用-h $host,单位是B)

redis-cli -h $host -p $port config set maxmemory 1024

2、获取实例的内存大小

redis-cli -h $host -p $port config get maxmemory

3、设置过期策略

redis-cli -h $host -p $port config set maxmemory-policy volatile-lru

4、查看过期策略

redis-cli -h $host -p 【本文来自鸿网互联 (http://www.68idc.cn)】$port config get maxmemory-policy

说明:测试过volatile-lru,确实在满了之后如果有设置了过期时间的key就会执行LRU,可以继续插入。如果没有设置了过期时间的key了就会报错

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn