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. Currently, 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 are dynamic or will be. The probability of happening this is dependent on the user's perspective and the requirements). In very common words I can define dynamic pages as including the following:
- Pages that directly interact with people
- Any media content
- Any type of graphic interaction
So, generally, these types of pages or websites are called dynamic. Now let's find out why we 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 overcome 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! 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 requirements, ASP.NET provides support for caching. Caching is the hero/heroine in this context that will help us to a great extent.
What is a Cache?
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 of catching
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 attribute 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 attribute; 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, 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 query string. For example.
<%@ OutputCache Duration = 5 VaryByParam = "*" %>
In the case of caching a page, some pages can generate different content for different browsers. In that scenario, we need to add an attribute to our statement to overcome 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 on the 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 the 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 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? 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 is 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!