Skip to content

Commit f232429

Browse files
committed
Implemented a much better lazy expiring algorithm for EXPIRE
1 parent 060f6be commit f232429

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

redis.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -940,14 +940,21 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD
940940
}
941941
}
942942

943-
/* Try to expire a few timed out keys */
943+
/* Try to expire a few timed out keys. The algorithm used is adaptive and
944+
* will use few CPU cycles if there are few expiring keys, otherwise
945+
* it will get more aggressive to avoid that too much memory is used by
946+
* keys that can be removed from the keyspace. */
944947
for (j = 0; j < server.dbnum; j++) {
948+
int expired;
945949
redisDb *db = server.db+j;
946-
int num = dictSize(db->expires);
947950

948-
if (num) {
951+
/* Continue to expire if at the end of the cycle more than 25%
952+
* of the keys were expired. */
953+
do {
954+
int num = dictSize(db->expires);
949955
time_t now = time(NULL);
950956

957+
expired = 0;
951958
if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
952959
num = REDIS_EXPIRELOOKUPS_PER_CRON;
953960
while (num--) {
@@ -958,9 +965,10 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD
958965
t = (time_t) dictGetEntryVal(de);
959966
if (now > t) {
960967
deleteKey(db,dictGetEntryKey(de));
968+
expired++;
961969
}
962970
}
963-
}
971+
} while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
964972
}
965973

966974
/* Check if we should connect to a MASTER */

0 commit comments

Comments
 (0)