Is it possible to iterate all OutputCache keys?

Is it possible to iterate the OutputCache keys? I know that you can delete them separately through HttpResponse.RemoveOutputCacheItem (), but is there a way so that I can iterate over all the keys to see what is in the collection?

I was looking for an Object Viewer but did not see anything.

In the worst case, I can save my own index. Since I do everything with VaryByCustom, they get "power" through a method in global.asax. It just seems to me that there should be a more elegant way to do this.

+4
source share
4 answers

If you are using ASP.NET 4.0, you can do this by writing your own OutputCacheProvider . This will give you the ability to store keys at the item’s cache point:

namespace StackOverflowOutputCacheProvider { public class SOOutputCacheProvider: OutputCacheProvider { public override object Add(string key, object entry, DateTime utcExpiry) { // Do something here to store the key // Persist the entry object in a persistence layer somewhere eg in-memory cache, database, file system return entry; } ... } } 

Then you can read the keys from the place where you saved them.

+2
source

It does.

 foreach (DictionaryEntry cachedItem in HttpContext.Current.Cache) { Response.Write(String.Format("<b>{0}:</b> {1}<br/>", cachedItem.Key.ToString(), cachedItem.Value.ToString())); } 
+1
source

I use this

http://www.codeproject.com/KB/session/exploresessionandcache.aspx

to view cache and session data. I only have to say that only data from one pool is shown. If you have more pools, you will only see the one you are on.

0
source

This can be done by inheriting from MemoryCache and exposing the enumerator through a custom implementation of OutputCacheProvider. Keep in mind that the enumerator blocks the cache. A cache listing should be infrequent.

 namespace Caching { internal class MemoryCacheInternal : System.Runtime.Caching.MemoryCache { public MemoryCacheInternal(string name, System.Collections.Specialized.NameValueCollection config = null) : base(name, config) { } public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, object>> Enumerator() { return base.GetEnumerator(); } } } 

Implement Custom OutputCacheProvider

 using System.Web.Caching; using System.Collections.Generic; namespace Caching { public class EnumerableMemoryOutputCacheProvider : OutputCacheProvider, IEnumerable<KeyValuePair<string, object>>, IDisposable { private static readonly MemoryCacheInternal _cache = new MemoryCacheInternal("EnumerableMemoryOutputCache"); public override object Add(string key, object entry, System.DateTime utcExpiry) { return _cache.AddOrGetExisting(key, entry, UtcDateTimeOffset(utcExpiry)); } public override object Get(string key) { return _cache.Get(key); } public override void Remove(string key) { _cache.Remove(key); } public override void Set(string key, object entry, System.DateTime utcExpiry) { _cache.Set(key, entry, UtcDateTimeOffset(utcExpiry)); } public IEnumerator<KeyValuePair<string,object>> GetEnumerator() { return _cache.Enumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _cache.Enumerator(); } private DateTimeOffset UtcDateTimeOffset(System.DateTime utcExpiry) { DateTimeOffset dtOffset = default(DateTimeOffset); if ((utcExpiry.Kind == DateTimeKind.Unspecified)) { dtOffset = DateTime.SpecifyKind(utcExpiry, DateTimeKind.Utc); } else { dtOffset = utcExpiry; } return dtOffset; } #region "IDisposable Support" // To detect redundant calls private bool disposedValue; // IDisposable protected virtual void Dispose(bool disposing) { if (!this.disposedValue) { if (disposing) { _cache.Dispose(); } } this.disposedValue = true; } public void Dispose() { // Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(true); GC.SuppressFinalize(this); } #endregion } } 

Configure Custom OutputCacheProvider

 <system.web> <caching> <outputCache defaultProvider="EnumerableMemoryCache"> <providers> <add name="EnumerableMemoryCache" type="Caching.EnumerableMemoryOutputCacheProvider, MyAssemblyName"/> </providers> </outputCache> <outputCacheSettings> <outputCacheProfiles> <add name="ContentAllParameters" enabled="false" duration="14400" location="Any" varyByParam="*"/> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web> 

Enumerate cache, in this case delete cache elements.

 OutputCacheProvider provider = OutputCache.Providers[OutputCache.DefaultProviderName]; if (provider == null) return; IEnumerable<KeyValuePair<string, object>> keyValuePairs = provider as IEnumerable<KeyValuePair<string, object>>; if (keyValuePairs == null) return; foreach (var keyValuePair in keyValuePairs) { provider.Remove(keyValuePair.Key); } 
0
source

Source: https://habr.com/ru/post/1312765/


All Articles