Key: firstname Value: samurai Expiry:6 hours
public static bool Add<T>(this IDatabase cache, string key, T value, TimeSpan? expiry = null)
{
dynamic response;
response = cache.StringSet(key, JsonConvert.SerializeObject(value, jsonSettings), expiry, flags: CommandFlags.DemandMaster);
}
return response;
}
Suppose at this moment there the TTL is 10mins. Ie. 5 hours and 50 mins have passed and the key will be deleted after 10 mins. But if someone performs a get operation on this key. The expiry should reset to 6 hours again.
One simple way to do this is to just perform cache.StringSet again. But I am afraid that may degrade performance under heavy load test. Are there any alternatives?
To achieve a sliding expiration, you can issue a KeyExpire[Async]
when you call StringGet
. Since you don't need to know the result of this, you can use FireAndForget
to avoid any latency cost - ie it won't count as a second round-trip:
cache.KeyExpire(key, TimeSpan.FromHours(6), CommandFlags.FireAndForget); var val = cache.StringGet(key);
Another approach would be to wrap this in Lua via ScriptEvaluate
and perform both operations there, but I don't think this is necessary here - the KeyExpire
and StringGet
pair should be fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.