Jul
25
2009

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());
		}
	}
}
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Comments

Leave a Comment

Login using OpenID or enter your details below to leave a comment.

OpenID
Anonymous


Comment

Powered by WP Hashcash