DCSIMG
Building a Simple Cache Manager - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Building a Simple Cache Manager

Building a Simple Cache Manager

As I wrote in a previous post,.Net Logo with ASP.NET
I’ve started to work in a
new project. My role
there is a technical team
leader of a very big team
(10 developers).
Part of my role is helping the
team to create infrastructure for their applications. As part of the application’s
infrastructure, I recommended to start using caching for better performance.
In the post I’ll demonstrate the simple cache manager I’ve built for the team.

ICacheManager Interface

Before building the cache manager I created an interface for caching.
I’ve done that in order to create abstraction between the application
and the used cache manager since in the future we may want to change
the implementation for Velocity distributed cache for example. Another
reason was for the testability of the caching subsystem. The last reason is
because we are going to use a dependency injection container for our
infrastructure. The following code is the interface I’ve created:

public interface ICacheManager
 {
     /// <summary>
     /// Add a new object into the cache
     /// </summary>
     /// <param name="key">The key of the object to add</param>
     /// <param name="value">The value of the object to add</param>
     void Add(string key, object value);
 
     /// <summary>
     /// Check whether the key is contained by the cache
     /// </summary>
     /// <param name="key">The key to check</param>
     /// <returns>Returns true if the key is contained by the cache</returns>
     bool Contains(string key);
 
     /// <summary>
     /// Returns the number of items in the cache
     /// </summary>
     /// <returns></returns>
     int Count();
 
     /// <summary>
     /// Insert a new object into the cache 
     /// </summary>
     /// <param name="key">The key of the object to insert</param>
     /// <param name="value">The value of the object to insert</param>
     void Insert(string key, object value);
    
     /// <summary>
     /// Get the object that its key is given
     /// </summary>
     /// <typeparam name="T">The object</typeparam>
     /// <param name="key">The given key to check</param>
     /// <returns>returns the object or null if it doesn't exists</returns>
     T Get<T>(string key);
 
     /// <summary>
     /// Removes the object that is referenced by the given key
     /// </summary>
     /// <param name="key">The given key</param>
     void Remove(string key);
 
     /// <summary>
     /// Get/Set the the given key and object
     /// </summary>
     /// <param name="key">The given key to the indexer</param>
     /// <returns>Returns the object that the given key reference</returns>
     object this[string key]
     {
         get;
         set;
     }
 }

The CacheManager Implementation

The implementation of the cache manager is very straight forward.
I’ve used the ASP.NET caching in order to cache the data. The following code
is the simple implementation I’ve created:

public class CacheManager : ICacheManager
{
    #region ICacheManager Members
 
    public void Add(string key, object value)
    {
        HttpRuntime.Cache.Add(key, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
    }
 
    public bool Contains(string key)
    {
        return HttpRuntime.Cache.Get(key) != null;
    }
 
    public int Count()
    {
        return HttpRuntime.Cache.Count;
    }
 
    public void Insert(string key, object value)
    {
        HttpRuntime.Cache.Insert(key, value);
    }
 
    public T Get<T>(string key)
    {
        return (T)HttpRuntime.Cache.Get(key);
    }
 
    public void Remove(string key)
    {
        HttpRuntime.Cache.Remove(key);
    }
 
    public object this[string key]
    {
        get
        {
            return HttpRuntime.Cache[key];
        }
        set
        {
            HttpRuntime.Cache[key] = value;
        }
    }
 
    #endregion
}

This isn’t going to be our caching manager in the future but for now
it is enough in order to start using cache in the application.

Summary

I’ve shown a very simple cache manager. I could have created other
solutions like having an in memory dictionary with cached data or other
implementation that I can think about but the ASP.NET caching is there
for using so why not use it. In the future this implementation will be
replaced with a more appropriate caching mechanism such as Velocity.

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 22, 2009 6:32 PM

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - August 20-24, 2009 Web Development ELMAH (Error

# August 23, 2009 6:20 PM