Caching in ASP.Net

It's a good habit to use caching in your application and coding standards too.

Outlines

  • Overview
  • Why Caching
  • What Cache does
  • Types
    • Page Caching
    • Fragment Caching
    • Data Caching

Overview

Caching is one of the most interesting concepts and operations in ASP.NET. If you can handle it, you can run any web application by applying the caching concept depending on the requirements.

Currrently a majority of websites/portals (or I can say simply web pages) are dynamic (if I do talk about a dynamic website, then it doesn't mean all the pages of that website is dynamic or will be. The probability of happening this is dependent on the user's perspective and the requirements).

In veru common words I can define dynamic pages as including the following:

    - Pages that directly interact with people
    - Communication (on page)
    - Any media content
    - Any type of graphic interaction

So, generally these types of pages or webs are called dynamic. Now let's find why we really need caching.

Why Caching

Caching is for providing solutions or the results to the users depending on their requested request, admin needs to recreate the pages often depending on user requests…STOP!!!

The process

The process is quite bulky and time-consuming. So to overcoming that problem some websites have a page creation engine that automatically creates all the pages in one action and directly saves those pages as a HTML structured page. These HTML pages serve the user depending on their requirements.

Multiple sorts of pages

But, do you still think this will be enough? If your answer is yes, then please think some more!

Actually, the preceding solution will only work if and only if the requested pages are of the same type. Now think, what will happen if the users request a different sort of page?

In that case your web will be stuck again.

So for dealing with that kind of complex but necessary requirements, ASP.NET provides support for caching. Caching is the hero/heroine in this context that will help us to a great extent.

What a Cache does


What a cache does, in the most simple words I can say is:

"A cache simply stores the output generated by a page in the memory and this saved output (cache) will serve us (users) in the future." That's it.

Types

caching

Page Caching

Let's explore the caching of an entire page, first.

To cache an entire page's output we need to specify a directive at the top of our page, this directive is the @ OutputCache.

Let's figure out a simple demo of it.

  1. <%@ OutputCache Duration = 5 VaryByParam = "ID" %>  
Here, in that statement Duration and VarByParam are the two attributes of the OutputCache directive. Now let's explore how they work. 
  • Duration Attribute

    This attributes represents the time in seconds of how long the output cache should be stored in memory. After the defined duration the content stored in the memory will be cleared automatically.

  • VarByParam Attribute

    This is the most important attributes; you can't afford to miss that in the OutputCache directory statement. It generally defines the query string parameters to vary the cache (in memory).

You can also multiple parameter names too, but for that you need to separate them using a semicolon (;).

You can also specify it as "*". In this case the cached content is varied for all the parameters ed using the querysrting.

For example:

  1. <%@ OutputCache Duration = 5 VaryByParam = "*"%>  
In case of caching a page, some pages can generate different content for different browsers. In that scenario we need to add an additional attribute to our statement for overcoming the preceding problem.

For example:
  1. <%@ OutputCache Duration = 5 VaryByParam = "ID" VaryByCustom = "Browser" %>  
Or:
  1. <%@ OutputCache Duration = 5 VaryByParam = "*" VaryByCustom = "Browser" %>  
Fragment caching

In some scenarios we only need to cache only a segment of a page. For example a contact us page in a main page will be the same for all the users and for that there is no need to cache the entire page.
 
So for that we prefer to use fragment caching option.

For example:
  1. <%@ OutputCache Duration = 10 VaryByParam = "None" %>  
Or:
  1. <%@ OutputCache Duration = 5 VaryByParam = "None" VaryByCustom = "Browser" %>  
Data Caching

Data caching is slightly different from the 2 other caching types. It's much more interesting to see how data caching actually works.

As we know in C# everything is about classes and objects. So ASP.NET supports data caching by treating them as small sets of objects. We can store objects in memory very easily and use them depending on our functionality and needs, anywhere across the page.

Now you must be thinking where is the class in that entire scenario?

Actually, this feature is implemented using the Cache class and data is treated as its object. Let's see how it works using a demo.

I am inserting a string value in the cache as:
  1. Cache["Website"] = "CSharpCorner";  
Now, for inserting the cache into the objects, the insert method of the Cache class can be used. This insert method is used as follows:
  1. Cache.Insert("Website", strName,  
  2. new CacheDependency(Sever.MapPath("Website.txt"));  
What we are missing something

We missed the Time for the cache (don't forget to use it), let's provide it:
  1. Cache.Insert("Website", strName,  
  2. new CacheDependency(Sever.MapPath("Website.txt")  
  3. DateTime.Now.Addminutes(5), TimeSpan.Zero);  
Happy coding!

Up Next
    Ebook Download
    View all
    Learn
    View all