LRU原理和Redis实现
作者:互联网
内存不够的场景下,淘汰旧内容的策略。LRU … Least Recent Used,淘汰掉最不经常使用的。可以稍微多补充两句,因为计算机体系结构中,最大的最可靠的存储是硬盘,它容量很大,并且内容可以固化,但是访问速度很慢,所以需要把使用的内容载入内存中;内存速度很快,但是容量有限,并且断电后内容会丢失,并且为了进一步提升性能,还有CPU内部的 L1 Cache,L2 Cache等概念。因为速度越快的地方,它的单位成本越高,容量越小,新的内容不断被载入,旧的内容肯定要被淘汰,所以就有这样的使用背景。
LRU原理
在一般标准的操作系统教材里,会用下面的方式来演示 LRU 原理,假设内存只能容纳3个页大小,按照 7 0 1 2 0 3 0 4 的次序访问页。假设内存按照栈的方式来描述访问时间,在上面的,是最近访问的,在下面的是,最远时间访问的,LRU就是这样工作的。
}
<span class="c1">// move the accessed node to the head;
this.moveToHead(node);
<span class="k">return</span> <span class="n">node</span><span class="o">.</span><span class="na">value</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">set</span><span class="o">(</span><span class="n">String</span> <span class="n">key</span><span class="o">,</span> <span class="kt">int</span> <span class="n">value</span><span class="o">)</span> <span class="o">{</span>
<span class="n">DLinkedNode</span> <span class="n">node</span> <span class="o">=</span> <span class="n">cache</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
<span class="k">if</span><span class="o">(</span><span class="n">node</span> <span class="o">==</span> <span class="kc">null</span><span class="o">){</span>
<span class="n">DLinkedNode</span> <span class="n">newNode</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DLinkedNode</span><span class="o">();</span>
<span class="n">newNode</span><span class="o">.</span><span class="na">key</span> <span class="o">=</span> <span class="n">key</span><span class="o">;</span>
<span class="n">newNode</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="na">cache</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="n">newNode</span><span class="o">);</span>
<span class="k">this</span><span class="o">.</span><span class="na">addNode</span><span class="o">(</span><span class="n">newNode</span><span class="o">);</span>
<span class="o">++</span><span class="n">count</span><span class="o">;</span>
<span class="k">if</span><span class="o">(</span><span class="n">count</span> <span class="o">></span> <span class="n">capacity</span><span class="o">){</span>
<span class="c1">// pop the tail
DLinkedNode tail = this.popTail();
this.cache.remove(tail.key);
–count;
}
}else{
// update the value.
node.value = value;
this.moveToHead(node);
}
}
/**
* Always add the new node right after head;
*/
private void addNode(DLinkedNode node){
node.pre = head;
node.post = head.post;
<span class="n">head</span><span class="o">.</span><span class="na">post</span><span class="o">.</span><span class="na">pre</span> <span class="o">=</span> <span class="n">node</span><span class="o">;</span>
<span class="n">head</span><span class="o">.</span><span class="na">post</span> <span class="o">=</span> <span class="n">node</span><span class="o">;</span>
<span class="o">}</span>
<span class="cm">/**
* Remove an existing node from the linked list.
*/
private void removeNode(DLinkedNode node){
DLinkedNode pre = node.pre;
DLinkedNode post = node.post;
<span class="n">pre</span><span class="o">.</span><span class="na">post</span> <span class="o">=</span> <span class="n">post</span><span class="o">;</span>
<span class="n">post</span><span class="o">.</span><span class="na">pre</span> <span class="o">=</span> <span class="n">pre</span><span class="o">;</span>
<span class="o">}</span>
<span class="cm">/**
* Move certain node in between to the head.
*/
private void moveToHead(DLinkedNode node){
this.removeNode(node);
this.addNode(node);
}
<span class="c1">// pop the current tail.
private DLinkedNode popTail(){
DLinkedNode res = tail.pre;
this.removeNode(res);
return res;
}
}
那么问题的后半部分,是 Redis 如何实现,这个问题这么问肯定是有坑的,那就是redis肯定不是这样实现的。
Redis的LRU实现
如果按照HashMap和双向链表实现,需要额外的存储存放 next 和 prev 指针,牺牲比较大的存储空间,显然是不划算的。所以Redis采用了一个近似的做法,就是随机取出若干个key,然后按照访问时间排序后,淘汰掉最不经常使用的,具体分析如下:
为了支持LRU,Redis 2.8.19中使用了一个全局的LRU时钟,server.lruclock
,定义如下,
#define REDIS_LRU_BITS 24
unsigned lruclock:REDIS_LRU_BITS; /* Clock for LRU eviction /
默认的LRU时钟的分辨率是1秒,可以通过改变REDIS_LRU_CLOCK_RESOLUTION
宏的值来改变,Redis会在serverCron()
中调用updateLRUClock
定期的更新LRU时钟,更新的频率和hz参数有关,默认为100ms
一次,如下,
#define REDIS_LRU_CLOCK_MAX ((1<<REDIS_LRU_BITS)-1) / Max value of obj->lru /
#define REDIS_LRU_CLOCK_RESOLUTION 1 / LRU clock resolution in seconds /
void updateLRUClock(void) {
server.lruclock = (server.unixtime / REDIS_LRU_CLOCK_RESOLUTION) &
REDIS_LRU_CLOCK_MAX;
}
server.unixtime
是系统当前的unix时间戳,当 lruclock 的值超出REDIS_LRU_CLOCK_MAX时,会从头开始计算,所以在计算一个key的最长没有访问时间时,可能key本身保存的lru访问时间会比当前的lrulock还要大,这个时候需要计算额外时间,如下,
/ Given an object returns the min number of seconds the object was never
* requested, using an approximated LRU algorithm. /
unsigned long estimateObjectIdleTime(robj o) {
if (server.lruclock >= o->lru) {
return (server.lruclock - o->lru) REDIS_LRU_CLOCK_RESOLUTION;
} else {
return ((REDIS_LRU_CLOCK_MAX - o->lru) + server.lruclock)
REDIS_LRU_CLOCK_RESOLUTION;
}
}
Redis支持和LRU相关淘汰策略包括,
volatile-lru
设置了过期时间的key参与近似的lru淘汰策略allkeys-lru
所有的key均参与近似的lru淘汰策略
当进行LRU淘汰时,Redis按如下方式进行的,
…
/* volatile-lru and allkeys-lru policy /
else if (server.maxmemory_policy REDIS_MAXMEMORY_ALLKEYS_LRU ||
server.maxmemory_policy REDIS_MAXMEMORY_VOLATILE_LRU)
{
for (k = 0; k < server.maxmemory_samples; k++) {
sds thiskey;
long thisval;
robj o;<span class="n">de</span> <span class="o">=</span> <span class="n">dictGetRandomKey</span><span class="p">(</span><span class="n">dict</span><span class="p">);</span> <span class="n">thiskey</span> <span class="o">=</span> <span class="n">dictGetKey</span><span class="p">(</span><span class="n">de</span><span class="p">);</span> <span class="cm">/* When policy is volatile-lru we need an additional lookup
* to locate the real key, as dict is set to db->expires. */
if (server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_LRU)
de = dictFind(db->dict, thiskey);
o = dictGetVal(de);
thisval = estimateObjectIdleTime(o);
<span class="cm">/* Higher idle time is better candidate for deletion */</span>
<span class="k">if</span> <span class="p">(</span><span class="n">bestkey</span> <span class="o">==</span> <span class="nb">NULL</span> <span class="o">||</span> <span class="n">thisval</span> <span class="o">></span> <span class="n">bestval</span><span class="p">)</span> <span class="p">{</span>
<span class="n">bestkey</span> <span class="o">=</span> <span class="n">thiskey</span><span class="p">;</span>
<span class="n">bestval</span> <span class="o">=</span> <span class="n">thisval</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">......</span>
Redis会基于server.maxmemory_samples
配置选取固定数目的key,然后比较它们的lru访问时间,然后淘汰最近最久没有访问的key,maxmemory_samples的值越大,Redis的近似LRU算法就越接近于严格LRU算法,但是相应消耗也变高,对性能有一定影响,样本值默认为5。
总结
看来,虽然一个简单的概念,在工业界的产品中,为了追求空间的利用率,也会采用权衡的实现方案。
标签:node,head,DLinkedNode,Redis,LRU,key,原理,REDIS 来源: https://blog.csdn.net/ALLENYYE/article/details/102775663