Click here to Skip to main content
13,597,963 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


56 bookmarked
Posted 21 Jul 2008
Licenced CPOL

Data Caching

, 2 Nov 2008
Rate this:
Please Sign up or sign in to vote.
Unified algorithm for caching data retrieved from a backend repository.


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:

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
            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
    #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();
                     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)

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:

private const string 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>(
            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.


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


About the Author

Dmitry Zubrilin
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralSQL Cache Pin
pradeepvp1234526-Mar-09 7:35
memberpradeepvp1234526-Mar-09 7:35 
GeneralRe: SQL Cache Pin
Dmitry Zubrilin26-Mar-09 14:08
memberDmitry Zubrilin26-Mar-09 14:08 
GeneralNot scallable Pin
Mileta Cekovic4-Nov-08 9:13
memberMileta Cekovic4-Nov-08 9:13 
GeneralRe: Not scallable Pin
Dmitry Zubrilin4-Nov-08 9:40
memberDmitry Zubrilin4-Nov-08 9:40 
Generalimplement cache Pin
Mojtaba Vali2-Nov-08 18:18
memberMojtaba Vali2-Nov-08 18:18 
GeneralRe: implement cache Pin
gil6662-Nov-08 23:00
membergil6662-Nov-08 23:00 
GeneralRe: implement cache Pin
Dmitry Zubrilin3-Nov-08 0:04
memberDmitry Zubrilin3-Nov-08 0:04 
GeneralRe: implement cache Pin
gil6663-Nov-08 1:28
membergil6663-Nov-08 1:28 
GeneralRe: implement cache [modified] Pin
Dmitry Zubrilin3-Nov-08 0:02
memberDmitry Zubrilin3-Nov-08 0:02 
GeneralRe: implement cache Pin
gil6663-Nov-08 1:50
membergil6663-Nov-08 1:50 
GeneralPlease Help [modified] Pin
gil6662-Nov-08 6:00
membergil6662-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 2 Nov 2008
Article Copyright 2008 by Dmitry Zubrilin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid