Hi,
In this article I’ll provide a simple way in order to build a Pluggable Generic ASP.NET Cache Manager.
I based my article on the John Sheehan article who make a pretty great work and Added Some Features to this Pluggable Cache Manager
- Thread Safe Singleton
- Multiple Cache Providers Support
- Multiple Cache Provider Instances
We often need to store and retrieve Cache Items, but there is a lot of Caching way : Cache, Session, Memcached, Velocity etc…
The is the “Magic” Interface which only provide 3 common methods
public interface ICacheProvider
{
void Save<T>(String key, T data);
void Remove(String key);
T Get<T>(String key);
}
Now the Implementation of the Cache Manager which will allow us to Register and Get Cache Providers.
public class CacheManager
{
#region Private Static Fields
private static Dictionary<String, ICacheProvider> _providers;
private static ReaderWriterLockSlim _lock;
#endregion
#region Singleton
static readonly CacheManager instance = new CacheManager();
static CacheManager()
{
_providers = new Dictionary<String, ICacheProvider>();
_lock = new ReaderWriterLockSlim();
}
CacheManager()
{
}
public static CacheManager Instance
{
get
{
return instance;
}
}
#endregion
#region Public Methods
public void RegisterCacheManager<T>(String cacheName) where T : ICacheProvider, new()
{
_lock.EnterUpgradeableReadLock();
if (!_providers.ContainsKey(cacheName))
{
_lock.EnterWriteLock();
T provider = new T();
_providers.Add(cacheName, provider);
_lock.ExitWriteLock();
}
_lock.ExitUpgradeableReadLock();
}
public void RegisterCacheManager<T>() where T : ICacheProvider, new()
{
RegisterCacheManager<T>(typeof(T).Name);
}
public T GetCacheManager<T>() where T : ICacheProvider
{
return GetCacheManager<T>(typeof(T).Name);
}
public T GetCacheManager<T>(String cacheName) where T : ICacheProvider
{
T provider = default(T);
_lock.EnterUpgradeableReadLock();
if (_providers.ContainsKey(cacheName))
{
provider = (T)_providers[cacheName];
}
else
{
throw new Exception("Unable to Locate Cache Manager");
}
_lock.ExitUpgradeableReadLock();
return provider;
}
#endregion
}
We have now the ICacheProvider interface and our CacheManager. Now let’s implements a simple CacheProvider for ASP.NET
public class MyCacheProvider : ICacheProvider
{
#region ICacheProvider Members
public void Save<T>(string key, T data)
{
HttpContext.Current.Items.Add(key, data);
}
public void Remove(string key)
{
HttpContext.Current.Items.Remove(key);
}
public T Get<T>(string key)
{
T data = default(T);
if (HttpContext.Current.Items[key] != null)
{
data = (T)HttpContext.Current.Items[key];
}
return data;
}
#endregion
}
We can now Register it in the CacheManager, Global.asax should be a great place for that.
protected void Application_Start(object sender, EventArgs e)
{
// Register Default Cache
CacheManager.Instance.RegisterCacheManager<MyCacheProvider>();
// Register Configuration Cache
CacheManager.Instance.RegisterCacheManager<MyCacheProvider>("Configuration");
}
So, we have created our Custom Cache Provider and Registered it into the Cache Manager. Now let’s use it in a sample web page.
Using Default Cache
public partial class _Default : Page
{
protected override void OnLoad(EventArgs e)
{
// Get Default Cache Provider
MyCacheProvider DefaultCache = CacheManager.Instance.GetCacheManager<MyCacheProvider>();
// Save something to the Cache
DefaultCache.Save("MyKey", "Hello !");
// Retrieve the Saved Value From Cache
String s = DefaultCache.Get<String>("MyKey");
Response.Write(s);
// Remove the Item from Cache
DefaultCache.Remove("MyKey");
base.OnLoad(e);
}
}
Using Configuration Cache
public partial class _Default : Page
{
protected override void OnLoad(EventArgs e)
{
// Get Configuration Cache Provider
MyCacheProvider ConfigCache = CacheManager.Instance.GetCacheManager<MyCacheProvider>("Configuration");
// Save something to the Cache
ConfigCache.Save("MyKey", "My Config Value");
// Retrieve the Saved Value From Cache
String s = ConfigCache.Get<String>("MyKey");
Response.Write(s);
// Remove the Item from Cache
ConfigCache.Remove("MyKey");
base.OnLoad(e);
}
}
Hope this will helpful :)
Views(3921)

