patterncsharpModerate
ASP.Net caching manager
Viewed 0 times
managercachingnetasp
Problem
I have a Cache Helper Class.
and usage:
```
string key = "EmployeeList";
List employees;
if (!CacheHelper.Get(key, out employees))
{
employees = DataAccess.GetEmployeeList();
CacheHelper.Add(employees, key);
Message.Text =
"Employees not
using System;
using System.Web;
public static class CacheHelper
{
///
/// Insert value into the cache using
/// appropriate name/value pairs
///
/// Type of cached item
/// Item to be cached
/// Name of item
public static void Add(T o, string key)
{
// NOTE: Apply expiration parameters as you see fit.
// I typically pull from configuration file.
// 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(1440),
System.Web.Caching.Cache.NoSlidingExpiration);
}
///
/// Remove item from cache
///
/// Name of cached item
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
}
///
/// Check for item in cache
///
/// Name of cached item
///
public static bool Exists(string key)
{
return HttpContext.Current.Cache[key] != null;
}
///
/// Retrieve cached item
///
/// Type of cached item
/// Name of cached item
/// Cached value. Default(T) if
/// item doesn't exist.
/// Cached item as type
public static bool Get(string key, out T value)
{
try
{
if (!Exists(key))
{
value = default(T);
return false;
}
value = (T) HttpContext.Current.Cache[key];
}
catch
{
value = default(T);
return false;
}
return true;
}
}and usage:
```
string key = "EmployeeList";
List employees;
if (!CacheHelper.Get(key, out employees))
{
employees = DataAccess.GetEmployeeList();
CacheHelper.Add(employees, key);
Message.Text =
"Employees not
Solution
///
/// Insert value into the cache using
/// appropriate name/value pairs
///
/// Type of cached item
/// Item to be cached
/// Name of item
public static void Add(T o, string key)I think this signature would be more useful like this:
///
/// Insert value into the cache using
/// appropriate name/value pairs.
///
/// Type of cached item (inferred from usage).
/// A string used to uniquely identify the added value.
/// Item/value to be cached.
public static void Add(string key, T value)This is misleading:
///
/// Remove item from cache
///
/// Name of cached item
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
}Clear is usually a parameterless method that clears a container's contents. This method should be named Remove.///
/// Check for item in cache
///
/// Name of cached item
///
public static bool Exists(string key)
{
return HttpContext.Current.Cache[key] != null;
}"Check for item in cache" doesn't really say what's going on. I realize it's trivially inferred from the method's name, but "Checks for existence of specified key in cache." would be totally unambiguous.
public static bool Get(string key, out T value)I don't like the implementation of that method very much, there are too many exit points, some being redundant. Consider something like this:
public static bool TryGet(string key, out T value)
{
bool result;
try
{
if (Exists(key))
{
value = (T)HttpContext.Current.Cache[key];
result = true;
}
}
catch(InvalidCastException)
{
value = default(T);
}
return result;
}Code Snippets
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs.
/// </summary>
/// <typeparam name="T">Type of cached item (inferred from usage).</typeparam>
/// <param name="key">A string used to uniquely identify the added value.</param>
/// <param name="value">Item/value to be cached.</param>
public static void Add<T>(string key, T value)/// <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)
{
return HttpContext.Current.Cache[key] != null;
}public static bool Get<T>(string key, out T value)Context
StackExchange Code Review Q#49541, answer score: 12
Revisions (0)
No revisions yet.