diff --git a/src/server.c b/src/server.c index e39ee4c74..aac39d983 100644 --- a/src/server.c +++ b/src/server.c @@ -864,18 +864,22 @@ void activeExpireCycle(int type) { if ((de = dictGetRandomKey(db->expires)) == NULL) break; ttl = dictGetSignedIntegerVal(de)-now; if (activeExpireCycleTryExpire(db,de,now)) expired++; - if (ttl < 0) ttl = 0; - ttl_sum += ttl; - ttl_samples++; + if (ttl > 0) { + /* We want the average TTL of keys yet not expired. */ + ttl_sum += ttl; + ttl_samples++; + } } /* Update the average TTL stats for this database. */ if (ttl_samples) { long long avg_ttl = ttl_sum/ttl_samples; + /* Do a simple running average with a few samples. + * We just use the current estimate with a weight of 2% + * and the previous estimate with a weight of 98%. */ if (db->avg_ttl == 0) db->avg_ttl = avg_ttl; - /* Smooth the value averaging with the previous one. */ - db->avg_ttl = (db->avg_ttl+avg_ttl)/2; + db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50); } /* We can't block forever here even if there are many keys to