Commit db5cdb17 authored by antirez's avatar antirez
Browse files

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.
parent 1200bbaf
...@@ -862,7 +862,21 @@ int expireIfNeeded(redisDb *db, robj *key) { ...@@ -862,7 +862,21 @@ int expireIfNeeded(redisDb *db, robj *key) {
* only the first time it is accessed and not in the middle of the * only the first time it is accessed and not in the middle of the
* script execution, making propagation to slaves / AOF consistent. * script execution, making propagation to slaves / AOF consistent.
* See issue #1525 on Github for more information. */ * 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: /* If we are running in the context of a slave, return ASAP:
* the slave key expiration is controlled by the master that will * the slave key expiration is controlled by the master that will
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment