Enumerating Cache Items

September 13th, 2006 Leave a comment Go to comments

System.Web.Caching.Cache object implements the IDictionary interface, hence we can retreive a list of cached item keys as follows:

protected ReadOnlyCollection GetCacheItemList()
{
    List cacheItemList = new List(Cache.Count);

    IDictionaryEnumerator cacheEnum = Cache.GetEnumerator();
    while (cacheEnum.MoveNext())
    {
        DictionaryEntry cacheItem = (DictionaryEntry)cacheEnum.Current;
        cacheItemList.Add(cacheItem.Key.ToString());
    }

    return new ReadOnlyCollection(cacheItemList);
}

  1. No comments yet.
  1. No trackbacks yet.
 

Comment moderation is enabled. Your comment may take some time to appear.