You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
publicclassMyService{privatereadonlyIMemoryCache_memoryCache;publicMyService(IMemoryCachememoryCache){_memoryCache=memoryCache;}publicvoidUpdateCachedItem(stringkey,intvalueChange,booladdFlag){varcurrentTTL= _memoryCache.GetExpirationTime(key);if(currentTTL.HasValue && _memoryCache.TryGetValue(key,outint 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}");}varcacheOptions=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.");}}}
The text was updated successfully, but these errors were encountered:
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.
Is there an existing issue for this?
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
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:
The text was updated successfully, but these errors were encountered: