Caching in ASP.NET

Without a doubt, caching can greatly improve performance on your website, or any other application. But if it isn’t done correctly, it can work against you.

Luckily, ASP.NET has a great built in cache manager in System.Web.Caching. It’s very easy to use, and it seamlessly handles (most of) the pitfalls that can get you into trouble. What’s nice about it is that the ASP.NET worker process will scavenge through your cache if it’s running low on memory.

So what is caching? Caching is taking a chunk of data and temporarily sticking it in any easy to reach place. For caching to work for you (instead of against you), that temporary place (called the “cache”) has to be easier to reach than the place you originally got the data. The cache is always dynamic and temporary.

So why does caching improve performance? Because you’re taking a hard to reach piece of data and making it temporarily easier to reach. It’s like this – if you’re driving through the streets of Saint Paul, MN, you’re going to need a map. If that map is in your backseat, you can temporarily cache it in your front passenger seat so it’s handy when you need to reference it. When you’ve left Saint Paul, you can throw in the backseat for next time. If you have a list of movie genres stored in your database that you need to access often, you can store that list in a memory cache. Because reading data from memory is much faster than reading it from a database, each request will be that much quicker.

So when should you cache? Basically, if you have a database or an I/O (hard drive read access) operation that needs to happen often for a broad range of requests, there’s a good chance that you can greatly improve performance by caching that data in memory the first time you grab it.

So when should you NOT cache? You shouldn’t cache data that is not used often. You shouldn’t cache data that is huge (this is kind of a judgment call). You shouldn’t use cache if you can’t use it judiciously. Remember – when you use caching in ASP.NET, you’re putting the object into memory. If you put too much stuff in memory, it can cause a lot of IIS application pool recycles; it can slow things down not only for your site, but for the entire Web server. Be nice to the cache and it will be nice to you.

Caching in ASP.NET C#

DataTable dtGenres = null;
try
{
    string cachekey = "Genres";
    dtGenres = (DataTable)HttpContext.Current.Cache[cachekey];
    if (dtGenres == null)
    {
        //the Genres weren't cached yet, so fetch the data, and add it to the cache for next time
        dtGenres = Genres.GetFromDB(); //put your own database code in here.
        HttpContext.Current.Cache.Insert(cachekey, dtGenres);
    }
}
catch (Exception ex)
{
    //handle exception
}

That’s about as basic as we can get with a cache example. Remember – it doesn’t have to be just DataTables that go into the cache. You can cache any object.

About these ads

One Comment

  1. eanirudh
    Posted November 3, 2008 at 11:19 am | Permalink

    great code


Post a Comment

Required fields are marked *

*
*

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: