31 July 2010

Caching output in asp.net to improve perfomance

Written by Amit Panchal, Posted in Blog

Hello friends,
this post is to explain how to use caching in asp.net to improve performance of application.
whenever you make any function in your asp.net 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))
        {
            try
            {
                //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 memory.it will not go inside to execute ur logic again.
i hope you enjoyed this.
if you have any problem in implementing this then just comment us

Social Bookmarks

About the Author

Amit Panchal

Co-Founder,

Amitech Business Solutions

All Events

Textify

Comments (0)

Leave a comment

You are commenting as guest. Optional login below.

Cancel Submitting comment...