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

adding a GetExpirationTime(Time-to-Live) method to the IMemoryCache #108626

Open
1 task done
MDshirzad opened this issue Oct 7, 2024 · 4 comments
Open
1 task done

adding a GetExpirationTime(Time-to-Live) method to the IMemoryCache #108626

MDshirzad opened this issue Oct 7, 2024 · 4 comments
Labels
area-Extensions-Caching untriaged New issue has not been triaged by the area owner

Comments

@MDshirzad
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I would like to suggest adding a GetExpirationTime(Time-to-Live) method to the IMemoryCache interface in Microsoft.Extensions.Caching.Memory. This method would allow developers to retrieve the remaining time before a cached item expires, which is currently not possible using the existing API.

At present, the IMemoryCache API allows us to add, retrieve, and remove items from the cache, but it doesn't expose any direct mechanism to retrieve the remaining expiration time (TTL) for a cached item. This feature is often useful in scenarios where cache invalidation and monitoring is important.

Developers currently have no way to determine how long a cache entry will last before it expires. This can be particularly limiting when we want to implement cache-aware features such as refreshing an entry before it expires or logging cache expiration diagnostics or if you want to update a value without changing the time.

Describe the solution you'd like

public interface IMemoryCache
{
    // Existing methods...

    TimeSpan? GetExpirationTime(string key);
}

I used redis for this option because the ttl was crucial for me and this inteface does not provide it,but the thing is that there are some points that I would rather to use IMemoryCache instead of redis:

  • Increased Complexity: Integrating Redis adds a technology layer, complicating development and deployment for small teams.

  • Infrastructure Overhead: Requires setup and maintenance of a separate Redis server, leading to added resource demands.

Additional context

This will be the usage:

public class MyService
{
    private readonly IMemoryCache _memoryCache;

    public MyService(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    public void UpdateCachedItem(string key, int valueChange, bool addFlag)
    {
        var currentTTL = _memoryCache.GetExpirationTime(key);

        if (currentTTL.HasValue && _memoryCache.TryGetValue(key, out int cachedValue))
        {
            if (addFlag)
            {
                cachedValue += valueChange;
                Console.WriteLine($"Adding {valueChange} to the cached value. New value: {cachedValue}");
            }
            else
            {
                cachedValue -= valueChange;
                Console.WriteLine($"Subtracting {valueChange} from the cached value. New value: {cachedValue}");
            }

            var cacheOptions = new MemoryCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = currentTTL.Value
            };

            _memoryCache.Set(key, cachedValue, cacheOptions);
            Console.WriteLine($"Cache item '{key}' updated with the same TTL of {currentTTL.Value.TotalMinutes} minutes.");
        }
        else
        {
            Console.WriteLine($"Cache item '{key}' does not exist or has no expiration time.");
        }
    }
}
@BrennanConroy BrennanConroy transferred this issue from dotnet/aspnetcore Oct 7, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 7, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-caching
See info in area-owners.md if you want to be subscribed.

@MDshirzad
Copy link
Author

MDshirzad commented Oct 8, 2024

@mgravell
@sebastienros
@dotnet/area-extensions-caching

@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Oct 8, 2024

Adding a GetExpirationTime method to interface IMemoryCache would be a breaking change. Other new features like #45593 were added only to class MemoryCache and not to the interface.

If the method were added, then the parameter should be object key, like in the existing bool TryGetValue(object key, out object? value).

MemoryCache supports "linked entries" that can expire together with other entries, before their own expiration times. I suppose GetExpirationTime would ignore that feature.

There are plans to replace IMemoryCache and MemoryCache, in #48567. That may make the team less willing to add features to MemoryCache. However, the planned replacement RCache<TKey, TValue> in dotnet/extensions#4766 (comment) doesn't have a GetExpirationTime method either.

@mgravell
Copy link
Member

mgravell commented Oct 8, 2024

What @KalleOlaviNiemitalo said :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Caching untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants