Closed GMacAussie closed 3 years ago
Hi @GMacAussie That is the expected behavior, if both cache layers have the same sliding expiration, it might never get refreshed. I'm usually using a shorter absolute-expiration in the first layer and longer sliding-expiration in the Redis layer.
If you still want to use the same cache expiration in both layers because you cannot configure them separately or want to use the per key expiration, you can "update" the sliding window in both layers by calling .Expire
, which will do a Get and then effectively a Put in both layers.
This operation is rather inefficient performance wise if you call that on every GET operation.
Expire
has to get the full data of the key, and then sends it to Redis to update the sliding windows. If you do that on every GET, you loose a lot of performance benefits of a 2 layered cache.
I highly recommend not to do that on every GET
But there are solutions for this which would work, you need a small time window in which the Expire
call would be skipped, but you'd have to make sure that the next call outside that window or the last call within the window would trigger an Expire
; otherwise you could miss one call.
That concept is called debouncing.
Unfortunately, it is nontrivial to implement debouncing properly and also very dependent on a use-case (needs configuration again at least). That's why this library doesn't handle stuff like that yet.
Thanks for the reply @MichaCo.
Debouncing sounds a bit fraught, and at the end of the day it just might be easier (and more reliable?) to use Redis as a single cache layer.
Thanks for again for your reply.
Hi there,
First, thank you for a great package!
We are experiencing an issue when using a distributed Redis configuration and a sliding window expiry, where accessing a key updates the sliding window only on the first access.
We are using this distributed Redis cache successfully for non-expiry, absolute expiry and sliding window when the cache key is regenerated on cache miss. This new use case uses the sliding window expiry as a gate (like a session cache).
Have we misunderstood the behaviour of a distributed Redis cache configuration with respect to sliding window expiration?
From the log, it appears that given the key is not in system cache, it will retrieve it from Redis with the sliding window update. The next access finds the key in system cache, and appears not to push the sliding window update(?). Note that the cache item itself has the expiration, not the cache configuration.
Here is our configuration:
Note that we suffix the
,allowAdmin=true
(also tried,useAdmin=true
) to the connection string, but we still get the message:However we do see keyspace events (see log below). Our Redis cache is a Azure Redis Standard C0 with the
notify-keyspace-events
set to AKE.This is the reproduction code (see attached solution):
Here is the NLog. I removed the cache startup preamble:
Solution:
CacheSliding.zip