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

Get all keys of items in cache #13

Open
andrewmurray opened this issue Sep 12, 2019 · 3 comments
Open

Get all keys of items in cache #13

andrewmurray opened this issue Sep 12, 2019 · 3 comments

Comments

@andrewmurray
Copy link

Is there a way to retrieve all of the keys of the items stored in cache?

@glav
Copy link
Owner

glav commented Sep 12, 2019

Not really no. Each cache engine has a different implementation and from memory, not each one supported it. There could be support added depending on the cache implenmentation tho and also added the a features supported. Alternatively for those that do not support it, we could use the parent-child relationship feature which tries to associate a parent key to multiple child keys and track it that way. A little inefficient in that scenario tho.

@Jogai
Copy link

Jogai commented Sep 13, 2019

Well, I did somethig like that for another memoryprovider. I'll share my code, maybe it helps you:

Using system.runtime cache is the easiest:

        using System.Runtime.Caching;

            List<string> cacheKeys = new List<string> { "supply", "known", "keys", "here" };

            MemoryCache currentCache = MemoryCache.Default;

            foreach (KeyValuePair<string, object> item in currentCache)
            {
                if (item.Key.StartsWith(String.Format(@"CachePrefix{0}", obj.Number)))
                {
                    cacheKeys.Add(item.Key);
                }
            }

            foreach (string cacheKey in cacheKeys)
            {
                currentCache.Remove(cacheKey);
            }

And for Microsoft.Extensions caching:

        using Microsoft.Extensions.Caching.Memory;
            List<string> cacheKeys = new List<string> { "supply", "known", "keys", "here" };

            //The next line is different, this depends if this cacheAdapter can do something like this
            MemoryCacheProvider provider = cache.CacheProvider as MemoryCacheProvider; 
            FieldInfo cacheInfo = provider.GetType().GetField("cache", BindingFlags.Instance | BindingFlags.NonPublic);
            MemoryCache currentCache = cacheInfo.GetValue(provider) as MemoryCache;

            PropertyInfo collectionInfo = currentCache.GetType().GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance);

            if (collectionInfo.GetValue(currentCache) is ICollection collection)
            {
                foreach (object item in collection)
                {
                    PropertyInfo keyInfo = item.GetType().GetProperty("Key");
                    string key = keyInfo.GetValue(item).ToString();
                    if (key.StartsWith(String.Format(@"CachePrefix{0}", obj.Number)))
                    {
                        cacheKeys.Add(key);
                    }
                }
            }

            foreach (string cacheKey in cacheKeys)
            {
                cache.Remove(cacheKey);
            }

@glav
Copy link
Owner

glav commented May 21, 2020

Thanks for this and apologies for the slow reply. Would be happy for you to PR this :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants