Today I will describes caching methods in ASP.NET. It's a good habit to use caching in your application and coding standards too.

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 grafic 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

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.

<%@ 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 pass 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 passed using the querysrting.
For example:

<%@ 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:

<%@ OutputCache Duration = 5 VaryByParam = "ID" VaryByCustom = "Browser" %> 

Or:

<%@ 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:

<%@ OutputCache Duration = 10 VaryByParam = "None" %> 

Or:

<%@ 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:

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:

Cache.Insert("Website", strName, 
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:

Cache.Insert("Website", strName, 
new CacheDependency(Sever.MapPath("Website.txt") 
DateTime.Now.Addminutes(5), TimeSpan.Zero); 

Happy coding!

TOP No#1 Recommended ASP.NET Hosting

ASPHostPortal.com

ASPHostPortal.com  is the leading provider of Windows hosting and affordable ASP.NET Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.