点击(此处)折叠或打开
-
server.lruclock = getLRUClock();
-
getLRUClock函数如下:
-
#define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */
-
#define LRU_BITS 24
-
#define LRU_CLOCK_MAX ((1<lru */
-
/* Return the LRU clock, based on the clock resolution. This is a time
-
* in a reduced-bits format that can be used to set and check the
-
* object->lru field of redisObject structures. */
-
-
unsigned int getLRUClock(void) {
-
return (mstime()/LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX;
-
}
因此lrulock最大能到(2**24-1)/3600/24 = 194天,如果超过了这个时间,lrulock重新开始。
对于redis server来说,server.lrulock表示的是一个全局的lrulock,那么对于每个redisObject都有一个自己的lrulock。这样每redisObject就可以根据自己的lrulock和全局的server.lrulock比较,来确定是否能够被淘汰掉。
redis key对应的value的存放对象:
点击(此处)折叠或打开
-
typedef struct redisObject {
-
unsigned type:4;
-
unsigned encoding:4;
-
unsigned lru:LRU_BITS; /* LRU time (relative to server.lruclock) or
-
* LFU data (least significant 8 bits frequency
-
* and most significant 16 bits decreas time). */
-
int refcount;
-
void *ptr;
-
} robj
那么什么时候,lru会被更新呢 ?访问该key,lru都会被更新,这样该key就能及时的被移动到lru头部,从而避免从lru中淘汰。下面是这一部分的实现:
点击(此处)折叠或打开
-
/* Low level key lookup API, not actually called directly from commands
-
* implementations that should instead rely on lookupKeyRead(),
-
* lookupKeyWrite() and lookupKeyReadWithFlags(). */
-
robj *lookupKey(redisDb *db, robj *key, int flags) {
-
dictEntry *de = dictFind(db->dict,key->ptr);
-
if (de) {
-
robj *val = dictGetVal(de);
-
-
/* Update the access time for the ageing algorithm.
-
* Don't do it if we have a saving child, as this will trigger
-
* a copy on write madness. */
-
if (server.rdb_child_pid == -1 &&
-
server.aof_child_pid == -1 &&
-
!(flags & LOOKUP_NOTOUCH))
-
{
-
if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {
-
unsigned long ldt = val->lru >> 8;
-
unsigned long counter = LFULogIncr(val->lru & 255);
-
val->lru = (ldt << 8) | counter;
-
} else {
-
val->lru = LRU_CLOCK();
-
}
-
}
-
return val;
-
} else {
-
return NULL;
-
}
-
}
接下来,我们在来分析,key的lru淘汰策略如何实现,分别有哪几种: (编辑:应用网_丽江站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|