MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / keyIsExpired

Function keyIsExpired

src/db.cpp:2030–2069  ·  view source on GitHub ↗

Check if the key is expired. Note, this does not check subexpires */

Source from the content-addressed store, hash-verified

2028
2029/* Check if the key is expired. Note, this does not check subexpires */
2030int keyIsExpired(const redisDbPersistentDataSnapshot *db, robj *key) {
2031 /* Don't expire anything while loading. It will be done later. */
2032 if (g_pserver->loading) return 0;
2033
2034 const expireEntry *pexpire = db->getExpire(key);
2035 mstime_t now;
2036 long long when;
2037
2038 if (pexpire == nullptr) return 0; /* No expire for this key */
2039
2040 if (!pexpire->FGetPrimaryExpire(&when))
2041 return 0;
2042
2043 /* If we are in the context of a Lua script, we pretend that time is
2044 * blocked to when the Lua script started. This way a key can expire
2045 * only the first time it is accessed and not in the middle of the
2046 * script execution, making propagation to slaves / AOF consistent.
2047 * See issue #1525 on Github for more information. */
2048 if (g_pserver->lua_caller) {
2049 now = g_pserver->lua_time_snapshot;
2050 }
2051 /* If we are in the middle of a command execution, we still want to use
2052 * a reference time that does not change: in that case we just use the
2053 * cached time, that we update before each call in the call() function.
2054 * This way we avoid that commands such as RPOPLPUSH or similar, that
2055 * may re-open the same key multiple times, can invalidate an already
2056 * open object in a next call, if the next call will see the key expired,
2057 * while the first did not. */
2058 else if (serverTL->fixed_time_expire > 0) {
2059 __atomic_load(&g_pserver->mstime, &now, __ATOMIC_ACQUIRE);
2060 }
2061 /* For the other cases, we want to use the most fresh time we have. */
2062 else {
2063 now = mstime();
2064 }
2065
2066 /* The key expired if the current (virtual or real) time is greater
2067 * than the expire time of the key. */
2068 return now > when;
2069}
2070
2071/* This function is called when we are going to perform some operation
2072 * in a given key, but such key may be already logically expired even if

Callers 4

lookupKeyReadFunction · 0.85
keysCommandCoreFunction · 0.85
expireIfNeededFunction · 0.85
isWatchedKeyExpiredFunction · 0.85

Calls 3

mstimeFunction · 0.70
getExpireMethod · 0.45
FGetPrimaryExpireMethod · 0.45

Tested by

no test coverage detected