-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Comments
Tagging subscribers to this area: @dotnet/area-extensions-caching |
@mgravell |
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 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. |
What @KalleOlaviNiemitalo said :) |
So what you suggest for the scenario that you wanna update an object without changing ttl in .net? |
You can store a mutable object, and update its fields while it's referenced by the cache. If the actual values you want to store are instances of a value type, then I don't think this indirection will even cost any extra CPU time or memory: the values would have to be boxed anyway, because MemoryCache is not generic and stores values as |
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: