In this post I am going to explain how to prevent the browser caching of web pages in asp.net. It is the one of the issues every developer faces.
Why browser caching?
To speed up the user experience on the web, most browsers implement a technology called caching. Caching allows information such as Web Pages, images, and so on, to be saved on a user’s computer. If the user calls for a previously requested web page, the browser is able to access the information more quickly by recalling it from a cache, rather than making another request to the site itself.
One side it is a advantage but when you display sensitive information it will be a big drawback .Sometimes it logs your Username and password.
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 of Catching
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 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.
<%@ 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.
<%@ OutputCache Duration = 5 VaryByParam = "ID" VaryByCustom = "Browser" %>
<%@ OutputCache Duration = 5 VaryByParam = "*" VaryByCustom = "Browser" %>
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.
<%@ OutputCache Duration = 10 VaryByParam = "None" %>
<%@ OutputCache Duration = 5 VaryByParam = "None" VaryByCustom = "Browser" %>
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"] = "codehunger";
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);