redis文档翻译_LRU缓存(一)

2015-07-24 07:47:10 · 作者: · 浏览: 0
  • ?
When Redis is used as a cache, sometimes it is handy to let it automatically evict old data as you add new one. This behavior is very well known in the community of developers, since it is the default behavior of the popular memcached system.
当使用Redis作为缓存时,有时候为了向缓存添加新数据,要让Redis自动删除旧数据。在社区,众所周知的 memcached 系统就有这种缺省的做法。
LRU is actually only one of the supported eviction methods. This page covers the more general topic of the Redis maxmemory directive that is used in order to limit the memory usage to a fixed amount, and it also covers in depth the LRU algorithm used by Redis, that is actually an approximation of the exact LRU.
LRU事实上是Redis唯一支持逐出的方法。此页包括Redis的maxmemory指令,用于限制Redis使用最大物理内存的大小,也包含Redis深度的LRU算法,实际是近似的LRU算法。

?

Maxmemory configuration directive 最大内存配置指令

The maxmemory configuration directive is used in order to configure Redis to use a specified amount of memory for the data set. It is possible to set the configuration directive using the redis.conf file, or later using the CONFIG SETcommand at runtime.
maxmemory 指令是用作配置指定Redis使用内存的最大值。可以在redis.conf文件配置,运行过程中还可以使用CONFIG SET 命令设置。
For example in order to configure a memory limit of 100 megabytes, the following directive can be used inside the redis.conf file.
例如,为了配置限制内存为100M,可以在redis.conf文件中配置:
maxmemory 100mb
Setting maxmemory to zero results into no memory limits. This is the default behavior for 64 bit systems, while 32 bit systems use an implicit memory limit of 3GB.
设置 maxmemory 为0 的结果是没有内存限制。这是64位系统的缺省做法,32位系统是限制3G。
When the specified amount of memory is reached, it is possible to select among different behaviors, called policies. Redis can just return errors for commands that could result in more memory being used, or it can evict some old data in order to return back to the specified limit every time new data is added. 当指定内存数量达到最大限制时,Redis可以选择不同的做法,称为 policies(政策)。
每当新的数据被添加时,可以只返回一个错误,或者可以逐出一些旧数据让新数据可以被添加。

?

Eviction policies 逐出政策

The exact behavior Redis follows when the maxmemory limit is reached is configured using the maxmemory-policy configuration directive.
当Redis内存到达上限时,Redis使用 maxmemory-policy 指令配置的政策执行。
The following policies are available: 下面是policies可以配置的 变量
noeviction: return errors when the memory limit was reached and the client is trying to execute commands that could result in more memory to be used (most write commands, but DEL and a few more exceptions).
noeviction:当达到最大内存使用限制并且客户端尝试执行使用更多内存的命令时,将返回一个错误。
allkeys-lru: evict keys trying to remove the less recently used (LRU) keys first, in order to make space for the new data added.
allkeys-lru:为了新添加的数据使用内存,将逐出最近最少使用的key。
volatile-lru: evict keys trying to remove the less recently used (LRU) keys first, but only among keys that have anexpire set, in order to make space for the new data added.
volatile-lru: 为了新添加的数据使用内存,将逐出有过期时间设置并且最近最少使用的key。
allkeys-random: evict random keys in order to make space for the new data added.
allkeys-random:: 为了新添加的数据使用内存,将随机逐出key。
volatile-random: evict random keys in order to make space for the new data added, but only evict keys with anexpire set.
volatile-random: 为了新添加的数据使用内存,将随机逐出具有过期时间设置的key。
volatile-ttl: In order to make space for the new data, evict only keys with an expire set, and try to evict keys with a shorter time to live (TTL) first.
为了新添加的数据使用内存,将逐出有过期时间限制,并且存活时间(TTL)最短的key。
The policies volatile-lru, volatile-random and volatile-ttl behave like noeviction if there are no keys to evict matching the pr