Friday, July 30, 2010

Caching output in to improve perfomance

Hello friends,
this post is to explain how to use caching in to improve performance of application.
whenever you make any function in your application you need to code it in the following way
public return_datatype Func_Name(datatype1 argument1,datatype2 argument2,datatype3 argument3,...)
        string cacheKey = keyarray("Func_Name", argument1,argument2,argument3......);
        object cacheItem = HttpContext.Current.Cache[cacheKey];
        if ((cacheItem == null))
                //logic of your actual function
               cacheItem=answer from your logic
            catch (Exception ex)
                cacheItem = (return_datatype) null;           }
            HttpContext.Current.Cache.Insert(cacheKey, cacheItem, null, absDate[put ur absolute date to expire cache memory], TimeSpan.Zero);
        return (return_datatype)(cacheItem);
public string keyarray(params object[] param)
        string keyarr = "";
        foreach (object i in param)
            keyarr += "&" + i.ToString();
        return keyarr;
you will need keyarr method to generate unique keyarr for perticular function call
all done.
now when u call func_name function with parameters like (1,2,3...) it will be cached and when u again call the same function with same parameters it will return the answer from the cache will not go inside to execute ur logic again.
i hope you enjoyed this.
if you have any problem in implementing this then just email me at
Amit Panchal

1 comment:

mgmtdocs said...

Nice post! Caching is wonderful technique to improve the performance of the app. But there are some build in problems in cache. Due to its stand alone and in process nature, it may results in scalability and reliability issues. This situation can become even worst if the data is distributed over multiple severs. this is a good read about performance .


Hell0 Friends,
i know you are stuck with some serious problems and that's why you are here. So friends i m putting all the solved problems(with solution) that i have faced in my life (technical problems) on this blog.
In case you can not find the proper solutions, feel free to mail me at
Amit Panchal