Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment




Latest Articles


Move to top
Increase performance of your website using caching
Posted By Sarin on Feb 06, 2013     RSS Feeds     Latest Hinduism news
1795 Views

Why Caching?
One of the best ways to improve performance of your website is to employ caching. Caching can be used to cache the whole page or a specific portion of your web page into memory. Caching can also be used to store the data where the data can be the records of your database. Caching helps us in avoiding the unnecessary database calls since the cached data can be used to display the retrieved results instead of calling the database for each user request.  
Because of caching, when the visitor request for the same page again then the Asp.Net worker process shows the cached result rather than the normal way of executing the code again to generate the HTML response and then send it back to the user. Caching not only helps in efficient utilization of our resources but also speedy and quick response to the user request.  
In this article, we will see the basic methods of output caching. Asp.net also allows some advanced caching technique but that would be unnecessary for small or medium sized websites. However, I would be covering those in my future articles.
  
Location attribute
One of the most important attribute in caching is location. This attribute let you define where you would like to store your cached results. Various options are
  1)    Client: Output cache will be stored on client browser
2)    Server:  Output client will be stored on web server.
3)    ServerAndClient: Asp.net worker process will decide where to store the result. It will either store the result on the client browser or on the web server.
4)    Downstream: Output cache will be stored on only HTTP 1.1 cache-capable devices. This includes proxy servers and the client machine that made the request.
5)    None: Disable caching for a particular control or the whole page 
6)    Any:  ASP.net worker process will decide where to cache the result. It can be either stored on the user browser or on the server or on the proxy server participating in the request.
    
How to implement Caching
Depending upon the functionality, there are various ways of implementing output caching in our application. We can cache our page based on the control, querystring, param, or our own custom implementation. Let us go through each one of them one by one:

Simplest way of caching
Simplest way to implement caching is to use the following chunk of code. Adding the below line on top of your aspx page will cache the entire content for 10 seconds. This means the ASP.net worker process will generate the HTML response once every 10 seconds and for the requests within these 10 seconds, it will show the cached results. Let us see it in action
<% @ OutputCache Duration="10" VaryByParam="none" %>


  
As you see in the above screenshot, I am refreshing my page continuously. For 10 seconds, I always see the cached result and only when ten seconds have passed, I see the new result on my browser.

Cache by VaryByParam  method
In the first approach, we have set the VaryByParam parameter to none which means no parameter is used to determine the cached result. In this approach we will set the VaryByParam Parameter to some value like ‘ID’. So, now for each varying Id, my HTML result will be cached in memory for the specified interval of time.  
For example, if my id in my querystring is set to 1, then a HTML response is generated for id equal to 1. Now if a new user comes and query with id equal to 1, then this generated HTML response will be displayed to the user but if the new user arrives and query with id equal to 2, then a new HTML response will be generated for this user. Now we have two cached pages in our memory. If a new user arrives with id equal to 1 or 2 then the cached result would be displayed to the user else a new HTML response is generated for this visitor and this response would be again cached into memory for duration specified in the duration property. Check it in action below
  
  <% @ OutputCache Duration="1000" VaryByParam="id" %>

As you see above, output is generated whenever I enter a new id. If I enter an id already entered before, then my cached result shows up.

Cache by VaryByControl method
In this approach, Asp.net tracks the change in a particular control to cache page. For example, in the below piece of code, my page is cached whenever the value is changed in the dropdown control named ‘ddlOption’. If I select the previously selected value in my dropdown, then my cached results show up. Check it in action below
<% @ OutputCache Duration="1000" VaryByControl="ddlOption" %>

  

Cache by VaryByCustom method
This is the last technique of caching page using outputcache directive. By this approach, you can define your own custom logic to cache your page. Instead of defining my own logic, I have set the value to ‘browser’ which means the result will be cached based on the client browser. For each browser, a separate result will be cached.

<% @ OutputCache Duration="1000" VaryByCustom="browser" VaryByParam="none" %>

Conclusion:
In this article, we saw the basic methods of output caching. In my next article, we will see some advanced methods of implementing caching on our websites.
Note: Images used on this website are either a production of Bhaktivedanta Book Trust(http://www.krishna.com), Iskcon Foundation or were found in google search under "Free to use and share". If any of the images presented here violates copyright issues or infringes anyone copyright or are not under "Fair use", then please bring it to our notice. Read Disclaimer for more.

Share this to your friends. One of your friend is waiting for your share.

Related Articles
Database cannot be opened due to inaccessible files or insufficient memory or disk space
How to encode and decode HTML request in ASP.NET
How AJAX Works, advantages and disadvantages
Give rounded corners to your panel using Ajax
Increase performance of your website using caching
Working with server controls and HMTL controls
Show Update Progress Animation-Ajax

Post Comment




Software developer by profession,
Poet by hobby,
Blogger by Passion
Birth Place: Munger, Bihar
Residence: Mumbai, Maharashtra
Prof exp: 8 years

Read More about me

Read my daily what'sapp quotes
Most Viewed Articles

Latest Spiritual Videos


Latest Funny Videos

Latest News

Latest Social Post
Latest WhatsApp Quotes