extensions: [API Proposal]: Add sliding expiration to `HybridCacheEntryOptions`

Background and motivation

This is a duplicate of https://github.com/dotnet/aspnetcore/issues/56754 which I believe is in the wrong repo.


Are there any plans to implement a sliding expiration for HybridCacheEntryOptions?

The existing DistributedCacheEntryOptions has a SlidingExpiration property which has been working well for my team and I. We have another mechanism to invalidate cache keys when updates to the underlying data occurs so we like to keep the data in Redis for as long as it is being used and eventually expire when that particular data is not regularly accessed.

Desired Solution

Ideally, the same SlidingExpiration property would be added to HybridCacheEntryOptions and the value would be passed along in ToDistributedCacheEntryOptions and handled in the downstream IDistributedCache calls.


API Proposal

namespace Microsoft.Extensions.Caching.Hybrid;

public sealed class HybridCacheEntryOptions
{
    public TimeSpan? SlidingExpiration { get; set; }
}

API Usage

// Set the sliding expiration
var entryOptions = new HybridCacheEntryOptions
{
    SlidingExpiration = TimeSpan.FromMinutes(5)
};

Risks

At first glance the risk involved here is implementing this in a manner that does not negate the performance benefits provided by using HybridCache.

For instance if a cache request hits the local MemoryCache it’s expiration would get updated but the entry in the IDistributedCache would not.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 13
  • Comments: 16 (9 by maintainers)

Most upvoted comments

You’re right this doesn’t exist today; something we’d need to consider is how to do this efficiently, i.e. how often do we trigger RefreshAsync when fetching something from L1 - is it every time? every N times? every N seconds? Conceptually I think this can work, though - both L1 and L2 have the concept of sliding expiration.

Well, the other way of spinning the delay is to keep a “last refreshed” (equally: “don’t refresh until” - same logic) token; so another entirely valid sequence is:

  • query comes in, miss in L1, hit from L2, stored in L1 (implicit refresh, set “last refreshed” to now)
  • (time passes)
  • query or queries comes in, hit from L1, but inside “last refreshed + delta” window; do nothing
  • query comes in, hit from L1, outside “last refreshed + delta” window, so: update “last refreshed” and issue async refresh
  • query comes in, hit from L1, now inside blah, do nothing

In your example above, this would mean we do perform a refresh at 0:04:50 (or any time after 0:00:20) - that work?

does the RedisCache impl support sliding expiration

Yes. All handled automatically inside the “get” APIs. Implemented as a “hash” in Redis - payload in one, non-absolute expiration data alongside.

No L1 sliding? Automatic L1 sliding when no L2?

Great question. I’d assume the latter, i.e. L1 sliding if no L2. But happy to discuss.

If using Redis pipelining, then it is possible to send an EXPIRE and GET in one roundtrip,

That misses the point; the backend fetch get should already trigger sliding expiration (although it is a little more nuanced than blind pipelining in the case of Redis) - however, the entire point of HybridCache is that we don’t use the distributed-cache every call, so we need some additional for L1 hits to cause L2 refreshes at some rate than isn’t necessarily “every L1 hit causes an L2 refresh”.