Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 4, 2013 12:14
Show Gist options
  • Select an option

  • Save ChrisMcKee/4452202 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4452202 to your computer and use it in GitHub Desktop.
namespace NUSSL.NUSExtra.Core.Caching
{
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.Caching;
/// <summary>
/// Cache manager - control entries to cache
/// </summary>
public abstract class TimedCacheManager
{
/// <summary>
/// Get an object from the cache
/// </summary>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(Cache cache, string key)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
return cache[key];
}
/// <summary>
/// Get an object by a pattern - it will return the first object that matches
/// the pattern
/// </summary>
/// <param name="cache"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static object GetByPattern(Cache cache, string pattern)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
IDictionaryEnumerator enumerator = cache.GetEnumerator();
Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
while (enumerator.MoveNext())
{
if (regex.IsMatch(enumerator.Key.ToString()))
return TimedCacheManager.Get(cache, enumerator.Key.ToString());
}
return null;
}
/// <summary>
/// Remove an object by key
/// </summary>
/// <param name="cache"></param>
/// <param name="key"></param>
public static void Remove(Cache cache, string key)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
cache.Remove(key);
}
/// <summary>
/// Remove all objects by a pattern
/// </summary>
/// <param name="cache"></param>
/// <param name="pattern"></param>
public static void RemoveByPattern(Cache cache, string pattern)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
IDictionaryEnumerator enumerator = cache.GetEnumerator();
Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
while (enumerator.MoveNext())
{
if (regex.IsMatch(enumerator.Key.ToString()))
TimedCacheManager.Remove(cache, enumerator.Key.ToString());
}
}
/// <summary>
/// Insert an item
/// </summary>
/// <param name="item"></param>
public static void Insert(Cache cache, string key, object value)
{
TimedCacheManager.Insert(cache, key, value, null);
}
/// <summary>
/// Insert an item
/// </summary>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dependencies"></param>
public static void Insert(Cache cache, string key, object value, CacheDependency dependencies)
{
TimedCacheManager.Insert(cache, key, value, dependencies, DateTime.MinValue);
}
/// <summary>
/// Insert an item
/// </summary>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dependencies"></param>
/// <param name="absoluteExpiration"></param>
public static void Insert(Cache cache, string key, object value, CacheDependency dependencies, DateTime absoluteExpiration)
{
TimedCacheManager.Insert(cache, key, value, dependencies, absoluteExpiration, TimeSpan.Zero);
}
/// <summary>
/// Insert an item
/// </summary>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dependencies"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
public static void Insert(Cache cache, string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
TimedCacheManager.Insert(cache, key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null);
}
/// <summary>
/// Insert an item
/// </summary>
/// <param name="cache"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dependencies"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
/// <param name="priority"></param>
/// <param name="onRemoveCallback"></param>
public static void Insert(Cache cache, string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
{
if (cache == null)
{
throw new ArgumentNullException("cache");
}
cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment