redis.c,第 2311 行,不稳定分支:
/* 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;
de = dictGetRandomKey(dict);
thiskey = dictGetKey(de);
/* When policy is volatile-lru we need an additonal 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);
/* Higher idle time is better candidate for deletion */
if (bestkey == NULL || thisval > bestval) {
bestkey = thiskey;
bestval = thisval;
}
}
}
严格来说,似乎所有事物都相同allkeys-lru
会更快,但幅度不会很大。我们谈论的机会可能不超过几分之一微秒。
第二个问题几乎已经得到回答,但以防万一:看起来有allkeys-lru
多少密钥设置为过期,或者如果有的话,没有区别。当 lru 算法清除密钥时,您的示例中的实例 A 和 B 都会看到相同的性能。