Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET
Article

Data Caching

Rate me:
Please Sign up or sign in to vote.
4.52/5 (15 votes)
2 Nov 2008CPOL3 min read 75.7K   56   12
Unified algorithm for caching data retrieved from a backend repository.

Introduction

Data retrieving from a repository can be quite a “heavy” task from a performance point of view, especially when the data repository is located far from the application server (e.g., web service call, RPC call, Remoting etc.) or some specific data is accessed very often. So, in order to reduce the workload and time for data retrieving, you can use a caching functionality.

There are some rules that you should be aware of:

  1. Use caching of data for a small period of time and avoid caching for the whole application lifecycle.
  2. Try to cache the data that is likely to not be changed very often (e.g., dictionary elements).
  3. Some data repositories can support notification events if the data is modified outside of the application (e.g., the data is just stored in a file).

Besides the obvious goals, data caching has some pitfalls (all of them are about potential situations when cached data can expire and application uses inconsistent data):

  1. If the application is going to be scaled to a distributed environment (web farm, application cluster), then every machine will have its own copy of cached data. So, one of the machines can modify the data at any time.
  2. Several applications can access the same data repository.

There are very many different implementations of the caching functionality, but all of them have a lot in common. I will use the “Microsoft Enterprise Library Caching Application Block” and will present you a way of simplifying the usage of the caching functionality.

My implementation of the caching functionality can be divided into two main parts: the cache manager utility and its usage. It heavily uses anonymous methods and generic methods (new features of .NET 2.0).

CacheService is a manager utility that is implemented as a singleton wrapper for underlying the MS caching API. It grants a few methods (GetData<t>, Add, Remove).

Here is the implementation of the caching manager utility:

C#
public delegate T GetCachedObjectDelegate<T><t>();

public sealed class CacheService
{
    #region singletone
    private static CacheService instance;
    private static readonly object instanceSync = new object();
    public static CacheService Current
    {
        get
        {
            if (CacheService.instance == null)
            {
                lock (CacheService.instanceSync)
                {
                    if (CacheService.instance == null)
                    {
                        CacheService.instance = new CacheService();
                    }
                }
            }
            return CacheService.instance;
        }
    }
    private CacheService()
    {
        this.cache = Microsoft.Practices.EnterpriseLibrary.Caching.CacheFactory
            .GetCacheManager();
    }
    #endregion singletone

    private Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager cache;

    public T GetData<T><t>(
            string key, object syncLock,
            TimeSpan timeout, GetCachedObjectDelegate<T><t> getCachedObject
        )
    {
        return this.GetData<t>(key, syncLock, null, timeout, getCachedObject);
    }
    public T GetData<t><T>(
            string key, object syncLock, 
            ICacheItemRefreshAction refreshAction,
            TimeSpan timeout, GetCachedObjectDelegate<T><t> getCachedObject)
    {
        object result = this.cache.GetData(key);
        if (result == null)
        {
            lock (syncLock)
            {
                result = this.cache.GetData(key);
                if (result == null)
                {
                    result = getCachedObject();
                    this.cache.Add(
                     key,
                     result,
                     CacheItemPriority.Normal,
                     refreshAction,
                     new SlidingTime(timeout)
                );
            }
        }
        return (T)result;
    }
    public void Add(
            string key, object value, 
            CacheItemPriority scavengingPriority, 
            ICacheItemRefreshAction refreshAction, 
            params ICacheItemExpiration[] expirations)
    {
        this.cache.Add(key, value, scavengingPriority, refreshAction, expirations);
    }
    public void Remove(string key)
    {
        this.cache.Remove(key);
    }
}

As you can see, this manager class does the following:

  1. It hides all the knowledge about the MS Caching Application Block.
  2. It presents very intuitive methods for working with the caching functionality.

Here is a sample usage:

C#
private const string GET_GOOD_INFO_BY_ID = 
        "GoodService.GoodInfo.GET_GOOD_INFO_BY_ID_";
private const double GET_GOOD_INFO_BY_ID_CACHE_INTERVAL = 0.2 * 60;
private static readonly object GET_GOOD_INFO_BY_ID_SINC_KEY = new object();

public GoodInfoModel GetGoodInfoByID(Guid id)
{
    string key = String.Concat(GoodService.GET_GOOD_INFO_BY_ID, id.ToString());
    return CacheService.Current.GetData<goodinfomodel>(
        key,
        GoodService.GET_GOOD_INFO_BY_ID_SINC_KEY,
        TimeSpan.FromSeconds(GoodService.GET_GOOD_INFO_BY_ID_CACHE_INTERVAL),
        delegate
        {
            return GoodDAL.GetGoodInfoByID(id);
        }
    );
}

As you can see, the usage of the caching functionality is quite simple. And, you can use any appropriate code in your application architecture inside the anonymous method so that it would be easy to add caching support to your application.

All you need is:

  1. Declare a synchronization object that will be used to synchronize access for the cached data;
  2. Define an expiration timeout;
  3. Use an anonymous method that must match the GetCachedObjectDelegate (remember that anonymous methods can access all the variables that are available in its outer method; actually, the C# compiler creates a new class definition that has all the variables as fields, and both the methods are just instance methods).

This implementation of the caching functionality can also be used for application scope caching. But, with a little effort, it can spawn the functionality for caching data at session or request scope.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat read Pin
rammanusani26-Mar-19 8:07
rammanusani26-Mar-19 8:07 
GeneralSQL Cache Pin
pradeepvpanzade26-Mar-09 7:35
pradeepvpanzade26-Mar-09 7:35 
GeneralRe: SQL Cache Pin
Dmitry Zubrilin26-Mar-09 14:08
Dmitry Zubrilin26-Mar-09 14:08 
GeneralNot scallable Pin
Mileta Cekovic4-Nov-08 9:13
Mileta Cekovic4-Nov-08 9:13 
GeneralRe: Not scallable Pin
Dmitry Zubrilin4-Nov-08 9:40
Dmitry Zubrilin4-Nov-08 9:40 
Generalimplement cache Pin
Mojtaba Vali2-Nov-08 18:18
Mojtaba Vali2-Nov-08 18:18 
GeneralRe: implement cache Pin
gil6662-Nov-08 23:00
gil6662-Nov-08 23:00 
GeneralRe: implement cache Pin
Dmitry Zubrilin3-Nov-08 0:04
Dmitry Zubrilin3-Nov-08 0:04 
You can find in a source code project a reference to MS entlib cache library. And it is used in a CacheService wrapper class.
GeneralRe: implement cache Pin
gil6663-Nov-08 1:28
gil6663-Nov-08 1:28 
GeneralRe: implement cache [modified] Pin
Dmitry Zubrilin3-Nov-08 0:02
Dmitry Zubrilin3-Nov-08 0:02 
GeneralRe: implement cache Pin
gil6663-Nov-08 1:50
gil6663-Nov-08 1:50 
GeneralPlease Help [modified] Pin
gil6662-Nov-08 6:00
gil6662-Nov-08 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.