Various Cache Clearing Methodologies

I tried to make this article cool, compact and simple. It contains various sorts of cache clearing approaches and their pros and cons with them. In spite of this I also merged browser cache clearing options too.

Outlines

  • Overview
  • Introduction
    • Scenarios
    • Cache vs History
  • Hierarchy
  • Types
  • Via Browser
  • Via Programming
    • Page Load Option
      • Approach: 1
      • Approach: 2

    • Into Master Page
  • Summary

Overview

This article is all about cache clearing using various methodologies, such as via browser and via programming. In this article you will learn how you can do that and what is the respective pros and cons of using a specific approach in your application.

So let's check what I have for you guys, this time.

Introduction

Let me first try to demonstrate, what actually this term "cache" means. (Please don't try to link it with browser history, they are different things.)

Scenarios

I am using several scenarios for explaining this.

Scenario: 1

Okay, whenever you try to make your account in some social networking sites, communities, forums and so on they always redirect you to a page called Signup/Registration and so on. Sometimes depending on that website functionality, during the filling in of the form if you take much more time then the page is automatically refreshed and the data fields are cleared.

Why?

Scenario: 2

Now think of a scenario in which you are accessing a website, for a fraction of time if anything goes wrong and your internet stops working and you refresh the page or press the back button then in this case you were still able to access (statically) that page or view its content (I am talking about content structure not design and other things).

Why?

There can be several scenarios like that (you must have encountered). This is all because of a cache. In the first scenario the cache clearing will be the hero and in the second the cache will be the hero.

I hope you understand what I tried to explain via this scenario.

Cache VS History

Cache and history are the two different things. Some people always gets confused with both. In this section I'll try to explain how they are different.



I hope the cache will be clear to you guys. I will now explain History via a simple scenario of the web browser.

Suppose you are accessing several website like C-Sharpcorner, Google, Gmail, Facebook and so on. After your work is done you closed your system and browser too. After a few minutes/hours/days/years you again open your system and open the same browser.

Now as you clicked on the address bar of your browser and typed C, G, F in 3 different tabs, what you get is shocking.

How did that the address bar get auto completed and you got all this stuff ?



Is it a miracle? Well no.

It's not a miracle, it's all because of "History". Whenever you do access anything in your web browser it is automatically stored in your web browser and the next time whenever you again try to access those websites (in the same browser), the above situation occurs.

You can check your browser history by clicking Ctrl+H or going through your browser's settings option in the menu.

Now I wish you guys are clear about the concept of a cache and history and the difference between them.

Hierarchy

In this hierarchy I did try to show you how and through what process you can easily clear a cache:



Types

There are two major approaches for cache clearing that people generally do use to increase their work experience during surfing or accessing any online content. I'll explain all these in the following categories.

(But before proceeding keep one thing in mind, both of the methods are for a different perspective and usage.) These two approaches are:

Via Browser

This is the default browser option. Generally all the browser provides these default functionalities in their settings. What we need to do is simply go to the settings and clear the cache.

This option helps in increasing browser speed. Here are some approaches of doing this via browser or we can say options in several browsers.

  • In Chrome
  • In Mozilla
  • In Safari
  • In IE

(Am not explain how you can do that using your default browser.. you can simply find the cache clearing option in the browser settings, go check them out.)

Via Programming

There are 2 options available to you, let's see them and their basic functionality, how they work and the types too:



Page Load Option

In this very first option you can write a code snippet directly into the page load event of a specific page or set of pages. In most cases we choose this type of approach in our application whenever these certain conditions are met:

  • Heavy Communication with server
  • Pages containing Forms, Forums and so on.
  • Suitable for Login/Logout pages
  • When we user to refill data again on refreshing a specific page

and so on.

There can be some other reasons too.

When to "USE"

As in the basics you can use this approach to n number of pages in the same application but there will be no use of it, because it will make your application slower.

So if your application has 1 or 2 pages that have user interaction and communication functionality then you can use this approach, it will be better for you as well as for your application's performance perspective.

Now for this option particularly I have structured two approaches, you can try any of them in your application. Let's explore them.

When to "NOT"

As I already said in the preceding paragraph if your application has more than 1-2 pages in which you want to apply caching, then don't use this approach.

Approach: 1

In this approach what you must do is simply set the response of each respective functionality that will be in the entire picture during communication.

Most commonly all these events are figured out using this statement:

  1. Response.Cache.OPERATION(Param); 

Here are various parameters for the scenarios that are in the entire picture, let's check them out.

  1. <!-- Starting of page load event -->  
  2.   
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5.   
  6. <!—Setting Cache Event -->  
  7.     Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  8.   
  9. <!—Setting Expiry Time through Systems Default Date-Time via DateTime Event -->  
  10.     Response.Cache.SetExpires(DateTime.Now);  
  11.   
  12. <!—Setting Server Caching Event -->  
  13.     Response.Cache.SetNoServerCaching();  
  14.   
  15. <!—Setting Erase or Clear Event -->  
  16.     Response.Cache.SetNoStore();  


Approach: 2

In this second approach the functionality is slightly different from the first approach. In this approach first of all we will set some default Date-Time value using system date and time and then a timer (depending on your application requirements), that will fire a time span.

After that time span the cache will be automatically cleared.

At last the control/cache clear statement will finalize all those things for you at no cost.

Let's go though the code.

  1. <!-- Starting of page load event -->  
  2.   
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5.   
  6. Response.ExpiresAbsolute = DateTime.Now.AddDays(-id);  
  7.   
  8. <!— Set according  to you -->  
  9. Response.Expires = -1500;  
  10.   
  11. <!— Control Event Statement -->  
  12. Response.CacheControl = "no-cache";  

Into master page

In this second method, we put our code snippet directly into the master page. This kind of functionality we use when we want the entire cache of the respective website to be cleared without delay.

When to "USE"

Mostly this approach is slow but works effectively. If you have several pages containg communication or user interactions then in this scenario don't use caching in each and every page, it will surely make your website slower and dumb.

I recommend you create a separate master page for all those pages and apply a master page cache clearing snippet in it. It will make your work effective and your application work faster and smooth.

When to "NOT"

If and only if, your web application has less than 2-3 pages that need that functionality, go for the page load option.

Approach: 1

In this option you need to set all the parameters and their respective value, as it is for a set of pages you need to set it to 0 and then other parameters. It uses this statement for functioning:

  1. <meta http-equiv = "Expires" Content = "0"

Now, let's explore the code:

  1. <!-- using master page -->  
  2. <meta http-equiv = "Expires" Content = "0">  
  3. <meta http-equiv = "Program" Content = "no-cache">  
  4. <meta http-equiv = "Cache-Control" Content = "no-cache"

Approach: 2

This second approach has cleared many milestones. For this you only need a single line of code and then you are done with the problem of the cache.

  1. Response.Cache.SetNoStore(); 

Summary

There can be some other options, snippets, procedures for doing that operation, but in this article I tried to keep it more simple and cozy.

If you are aware of or find any other relevant approaches then let me know, I'll update my article.

Until then, enjoy coding!

Have Fun!


Similar Articles