Try wrapping the Cache class:
Code:
public class MyCache
{
private static readonly System.Web.Caching.Cache m_cache;
///<summary>
/// Static initializer should ensure we only have to look up the current cache
/// instance once.
///</summary>
static MyCache()
{
HttpContext context = HttpContext.Current;
if (context != null)
{
m_cache = context.Cache;
}
else
{
m_cache = HttpRuntime.Cache;
}
}
///<summary>
/// Removes all items from the Cache
///</summary>
public static void Clear()
{
IDictionaryEnumerator CacheEnum = m_cache.GetEnumerator();
while (CacheEnum.MoveNext())
m_cache.Remove(CacheEnum.Key.ToString());
}
///<summary>
/// Removes the specified key from the cache
///</summary>
///<param name="key"></param>
public static void Remove(string key)
{
m_cache.Remove(key);
}
///<summary>
/// Insert the current "obj" into the cache.
///</summary>
///<param name="key"></param>
///<param name="obj"></param>
public static void Insert(string key, object obj)
{
Insert(key, obj, null, 1);
}
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
}
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
{
if (obj != null)
{
m_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(m_factor * seconds), TimeSpan.Zero, priority, null);
}
}
public static object Get(string key)
{
return m_cache[key];
}
}
I should also point out that doing this allows you to use the cache from Windows Forms applications.
Bookmarks