dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.08k stars 4.7k forks source link

[API Proposal]: Extend IMemoryCache to also provide keys/provider info #98251

Closed thompson-tomo closed 2 months ago

thompson-tomo commented 8 months ago

Background and motivation

I like what has been done with #36554 however for me it is not enough to be able to implement an actuator to provide information about the cache as done in spring boot.

I would like the method to be available as part of the interface so that i have a consistent way to get the keys and the metadata regardless of the provider.

API Proposal

Interface extension

namespace System.Collections.Generic;

IEnumerable<CacheItem> GetCacheItems => new IEnumerable<CacheItem>();

new class

public class CacheItem
{
    public string ProviderName
    public string StorageType
    public object Key
}

API Usage


foreach (var item in _memoryCache.GetCacheItems.ToList())
{
    //build object as per actuator needs which includes 
}

Alternative Designs

Risks

Should be low risk as not a breaking change by providing default implementation.

ghost commented 8 months ago

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

Issue Details
### Background and motivation I like what has been done with #36554 however for me it is not enough to be able to implement an actuator to provide information about the cache as done in spring boot. I would like the method to be available as part of the interface so that i have a consistent way to get the keys and the metadata regardless of the provider. ### API Proposal Interface extension ```csharp namespace System.Collections.Generic; IEnumerable GetCacheKeys => new IEnumerable(); ``` new class ```csharp public class CacheItem { public string ProviderName public string StorageType public object Key } ``` ### API Usage ```csharp foreach (var item in _memoryCache.GetCacheKeys.ToList()) { //build object as per actuator needs which includes } ``` ### Alternative Designs _No response_ ### Risks Should be low risk as not a breaking change by providing default implementation.
Author: thompson-tomo
Assignees: -
Labels: `api-suggestion`, `untriaged`, `area-Extensions-Caching`
Milestone: -
jozkee commented 3 months ago

Is CacheItem a view of a CacheEntry? What's ProviderName and StorageType? Can you be more specific about the extension method you are proposing here? In which static type will it live?

dotnet-policy-service[bot] commented 3 months ago

This issue has been marked needs-author-action and may be missing some important information.

julealgon commented 3 months ago

@thompson-tomo if you want to know what happens with the IMemoryCache object, why don't you just create a simple decorator for it that intercepts the calls and stores the information you need before/after each method is executed?

From your description, introspection on the calls (and callbacks) should be enough to be able to provide a "view" of the cache.

Also, instead of viewing the entire state of the cache, I believe it would be better to have metrics around the cache so that you could graph them in observability tools.

dotnet-policy-service[bot] commented 3 months ago

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

dotnet-policy-service[bot] commented 2 months ago

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.