ASP.NET: Remove from the cache using
Comments available as RSS 2.0
The Cache object in ASP.NET isn’t like other collections, probably because it needs to be specialized so it doesn’t make use of generics. Unfortunately, that makes it more difficult to work with. This small utility method removes an object from the cache by key using a predictate. You could easily modify it to compare either of the IDictionaryEnumerator’s Key or Value properties.
/// <summary> /// Removes an item from the cache using a Predictate to match the key. /// </summary> /// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param> /// <summary> /// Removes an item from the cache using a Predictate to match the key. /// </summary> /// <param name="keyCriteria">The Predictate to use for each key to determine whether the entry should be removed.</param> public static void RemoveFromCache(Predicate<string> keyCriteria) { IDictionaryEnumerator enuma = HttpContext.Current.Cache.GetEnumerator(); while(enuma.MoveNext()) { if(keyCriteria(enuma.Key.ToString())) { HttpContext.Current.Cache.Remove(enuma.Key.ToString()); } } }

Comments
Leave a Comment