redis允許對(duì)key設(shè)置超時(shí)時(shí)間,實(shí)現(xiàn)過(guò)期key的自動(dòng)淘汰。這篇blog分析下,其自適應(yīng)(adaptive)的淘汰機(jī)制。
redis每隔100ms定時(shí)執(zhí)行的循環(huán)(serverCron function)里有如下語(yǔ)句:
655
/*
Expire a few keys per cycle, only if this is a master. 656 * On slaves we wait for DEL operations synthesized by the master 657 * in order to guarantee a strict consistency.
*/
658
if
(server.masterhost == NULL) activeExpireCycle();
正如文中注釋所示,只有master執(zhí)行expire cycle,slave會(huì)等候由master傳遞的DEL消息,保證master-slave在過(guò)期值處理上的一致性。(后邊代碼會(huì)看到,redis對(duì)過(guò)期值的選擇是隨機(jī)抽取的,master-slave完全可能抽取不同的值,因此要求master通過(guò)DEL消息實(shí)現(xiàn)同步,同時(shí)這種expire機(jī)制也是不可靠的expire,即key超時(shí)后有可能不會(huì)被刪除)。
activeExpireCycle函數(shù)如下:
477
/*
Try to expire a few timed out keys. The algorithm used is adaptive and 478 * will use few CPU cycles if there are few expiring keys, otherwise 479 * it will get more aggressive to avoid that too much memory is used by 480 * keys that can be removed from the keyspace.
*/
481
void
activeExpireCycle(
void
) {
482
int
j;
483
484
for
(j =
0
; j < server.dbnum; j++
) {
485
int
expired;
486
redisDb *db = server.db+
j;
487
488
/*
Continue to expire if at the end of the cycle more than 25% 489 * of the keys were expired.
*/
490
do
{
491
long
num = dictSize(db->
expires);
492
time_t now =
time(NULL);
493
494
expired =
0
;
495
if
(num >
REDIS_EXPIRELOOKUPS_PER_CRON)
496
num =
REDIS_EXPIRELOOKUPS_PER_CRON;
497
while
(num--
) {
498
dictEntry *
de;
499
time_t t;
500
501
if
((de = dictGetRandomKey(db->expires)) == NULL)
break
;
502
t =
(time_t) dictGetEntryVal(de);
503
if
(now >
t) {
504
sds key =
dictGetEntryKey(de);
505
robj *keyobj =
createStringObject(key,sdslen(key));
506
507
propagateExpire(db,keyobj); //將刪除操作傳播給各個(gè)slaves,在此之前,還將del操作記錄aof
508
dbDelete(db,keyobj); //這個(gè)函數(shù)先從db->expires中刪除,然后刪除db->dict
509
decrRefCount(keyobj);
510
expired++
;
511
server.stat_expiredkeys++
;
512
}
513
}
514
}
while
(expired > REDIS_EXPIRELOOKUPS_PER_CRON/
4
);
515
}
516
}
ExpireCycle每次嘗試處理10個(gè)key,如果10個(gè)key中有>2.5個(gè)超時(shí),則繼續(xù)處理10個(gè)key。其用意在于,如果超時(shí)的key比例很高,則一次迭代處理很多個(gè),否則等待下次serverCron循環(huán)再隨機(jī)抽取。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

