4

Is it possible to iterate the OutputCache keys? I know you can remove them individually via HttpResponse.RemoveOutputCacheItem(), but is there a way I can iterate all the keys to see what's in the collection?

I searched through Object Viewer, but didn't see anything.

Worst case, I can maintain my own index. Since I'm doing everything by VaryByCustom, they get "fed" through a method in global.asax. It just strikes me that there has to be a more elegant way of doing this.

Deane
  • 8,269
  • 12
  • 58
  • 108

3 Answers3

3

If you're using ASP.NET 4.0 you could do this by writing your own OutputCacheProvider. That would give you the ability to store the keys at the point that the item is cached:

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 e.g. in-memory cache, database, file system

            return entry;
        }

        ...

    }
}

You'd then be able to read the keys out from wherever you've stored them.

PhilPursglove
  • 12,511
  • 5
  • 46
  • 68
1

This can be accomplished by inheriting MemoryCache and exposing the enumerator via a custom OutputCacheProvider implementation. Keep in mind that the enumerator locks the cache. Enumerating over the cache should be executed infrequently.

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 a 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 the 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 over the cache, in this case removing cache items.

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);
}
Richard Collette
  • 5,462
  • 4
  • 53
  • 79
0

I have use this

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

to view the cache and the session data. I only have to say that show only one pools data. If you have more pools, then you just see the one you are on it.

Aristos
  • 66,005
  • 16
  • 114
  • 150