I am using Umbraco caching using Umbraco.Core.Cache;
I have no problem getting cache item using this line of code
ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem(
given the correct cache key item.
Now my question is:
What if I forgot the cache key item? is there any way I can peek for all cache item? Or for debugging purpose I just want to see all of them? I traced all possible intellisense suggestion but seems no "GetAllCacheItem" available
Anyone please enlighten me is it possible?
When writing the code you could only get intellisense on the cache keys if you used constants (like below) but the drawback is having to maintain the constant values when adding new cache items.
ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem(CacheKeys.SAMPLE_KEY)
public class CacheKeys
{
public const string SAMPLE_KEY = "some-example-key";
}
Whilst debugging you are able to view the cache keys as follows; under the hood IRuntimeCacheProvider
(ApplicationContext.ApplicationCache.RuntimeCache
) uses the HttpRuntime
cache so although you cannot iterate over cache items in the RuntimeCache
property directly you can use HttpRuntime.Cache
like:
var keys = new StringBuilder();
foreach (DictionaryEntry cacheItem in HttpRuntime.Cache)
{
keys.AppendLine(cacheItem.Key.ToString());
}
Items added to the runtime cache via the Umbraco provider contain the prefix "umbrtmche-" so you may wish to filter the results:
HttpRuntime.Cache.Cast<DictionaryEntry>()
.Where(x => x.Key.ToString().StartsWith("umbrtmche"))
.Select(x => x.Key.ToString().Replace("umbrtmche-", ""))
.ToList();
And the final thing to note is that Umbraco uses the cache itself so you will not only see cache keys you have added, if you wish to filter these I suggest adding a prefix of your own so that you can filter your own cache keys from Umbraco's.