Avoid gettimeofday() in expireIfNeeded() when possible.

When the key expires far in the future compared to the cached time in
server.mstime, calling mstime(), that calls gettimeofday(), should not
be very useful. Instead when we are near the expire, we want the
additional precision.

This commit is related to issue #2552.
This commit is contained in:
antirez 2015-04-29 15:17:29 +02:00
parent 1200bbafd1
commit db5cdb174b

View File

@ -862,7 +862,21 @@ int expireIfNeeded(redisDb *db, robj *key) {
* only the first time it is accessed and not in the middle of the
* script execution, making propagation to slaves / AOF consistent.
* See issue #1525 on Github for more information. */
now = server.lua_caller ? server.lua_time_start : mstime();
if (server.lua_caller) {
now = server.lua_time_start;
} else {
/* If this is not the Lua caller, we actually need to get the current
* time. However gettimeofday(), which is called by mstime(), may be
* expensive, so we try to use the cached time instead, as found in
* server.mstime, which is not very accurate, but should usually be
* in the range of +/- 100 milliseconds.
*
* If the time the key will expire seems to be much more in the future
* compared to server.mstime, we use the server.mstime approximation.
* Otherwise if we see the key is going to expire within two seconds
* we fetch the actual time from the operating system. */
now = (when - server.mstime > 2000) ? server.mstime : mstime();
}
/* If we are running in the context of a slave, return ASAP:
* the slave key expiration is controlled by the master that will