Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Add sliding expiration to HybridCacheEntryOptions #5649

Open
CoryCharlton opened this issue Nov 15, 2024 · 1 comment
Open
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged

Comments

@CoryCharlton
Copy link

CoryCharlton commented Nov 15, 2024

Background and motivation

This is a duplicate of dotnet/aspnetcore#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.

@CoryCharlton CoryCharlton added api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged labels Nov 15, 2024
@mvxproject
Copy link

In the case when we get the cache from MemoryCache we can refresh the distributed cache before returning the value.

_backendCache.RefreshAsync(key, token);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged
Projects
None yet
Development

No branches or pull requests

2 participants