A cache manager class.
public class OxxCacheManager
{
public static void Add<T>(T o, string key, int absoluteExpireMinutesFromNow = 120) where T : class
{
// NOTE: Apply expiration parameters as you see fit.
// In this example, I want an absolute
// timeout so changes will always be reflected
// at that time. Hence, the NoSlidingExpiration.
HttpContext.Current.Cache.Insert(
key,
o,
null,
DateTime.Now.AddMinutes(absoluteExpireMinutesFromNow),
System.Web.Caching.Cache.NoSlidingExpiration);
}
/// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
}
/// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public static bool Exists(string key)
{
if(HttpContext.Current != null && HttpContext.Current.Cache != null)
{
return HttpContext.Current.Cache[key] != null;
}
return false;
}
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <returns>Cached item as type</returns>
public static T Get<T>(string key) where T : class
{
try
{
return (T)HttpContext.Current.Cache[key];
}
catch
{
return null;
}
}
}