Using Caching to make your ASP.NET website more scalable


Using Caching to make your ASP.NET website more scalable

Posted on Jul 3, 2008 by Paul White

Even though I have known about caching for a while.  I had yet to implement it within any of my websites.  But I had a new project that I could see would definately benefit from caching. 

What is Caching?


The easiest way to think about caching is like an object stored into the application Object but with a expiration similar to a cookie.  You can store anything you want in the ASP.NET cache. 

Why use Caching?


Most Dynamic websites are driven by a Database, be it SQL, Oracle, or XML.  Each Call to the Database results in a performance hit.  Many times the Database might be on another server within the network. This means you are forced to go outside the CPU of your server to the network that is 100's of times slower.  By using Caching you avoid these performance hits since you keep all the processing within the confines of your server.  Caching can allow a server that normally would only be able to handle 5000 visitors / day, to now handle 100,000 visitors / day. 

When to use Caching?


When I build a website I break up everything into smaller objects.  I might have a small function that creates a list of categories on the server, and then returns the list as a string.  I then take this string and set a Label on my page to its value.  I might repeat this on every page on my site, generating a nice list of categories to be displayed on the right side of my browser.    These categories might not change very often.  They only change once a month if that.  This would be a great place to implement a cache.

When to not use Caching?


In situations that the value might be different every time, such as a hit counter, or rotating banner ads.  Unless you want to set a Cache that would expire every minute or so, allowing the data to never be outdated by more than a few minutes. 

How to use Caching?


We will start with my function getCategoriesList()
Normally it would opperate like this
Start getCategoriesList(){
   query SQL for list
   Parse together string 
   Return String
}

The result is every time the function is called it hits the Database with a query.  This might not be a big deal when the traffic on your website is low, and your SQL query is simple, but if you start dealing with high traffic websites, and complex queries this can cause a bottle neck.    When can modify our function to opperate like so

Start getCategoriesList(){
   Check Cache for getCategoriesList string
   If Cache not found then create it
      query SQL for list
      Parse together string 
      Create Cache and set equal to String, set to expire in 12 hours.
   }
   return Cache Object
}

We have now taken a function and modified it so it will create a cache and refresh this cache every 12 hours.  The result is instead of the function having to goto the network and query SQL each time it will do it once, then cache the result in memory.  Now in your code when it calls up the getCategoriesList() function it just has to pull the Cached value from your servers memory. 

Real Code Examples

/////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
public void addToCache(string cachename, string data, int hours){
    Cache.Add(cachename, data, null, DateTime.Now.AddHours(hours), TimeSpan.Zero, CacheItemPriority.NotRemovable,null);
}
/////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
public string getFromCache(string cachename){
    return (string)Cache[cachename];

}
/////////////////////////////////////////////////////////////////////
//  get Blog Name
////////////////////////////////////////////////////////////////////
    public string getBlogName(string blogid){


// create a unique name for your cached object
        string cachename="getBlogName_"+blogid;

// check cache to see if object is already loaded
        if(getFromCache(cachename)!=null){

// if object was not null return it
return getFromCache(cachename);
}

// else object was not found so query database to get it
        string data=countDB3("SELECT title FROM blogs where id='"+blogid+"'");

// store object in cache so we don't have to hit the Database next time
        addToCache(cachename, data, 12);

// return object
        return data;
    }




Permalink
7587 Visitors
12873 Views

Categories associated with Using Caching to make your ASP.NET website more scalable

Discussion

Leo | Apr 20, 2011 7:44 AM
the cache in asp.net is an InProc cache, as it may ends up with some performance issues if your app is running in a multi-server environment. so the of an in-memory distributed cache can be a better option if the number of servers are two or more.  
name
Email Needed to confirm comment, but not made public.
Website
 
Type Code
Security Check
 
When you Post your Comment, you'll be sent a confirmation link. Once you click this link your thoughts will be made public.. Posts that are considered spam will be deleted, Please keep your thoughts and links relavent to this Article