Home | HTML | Asp.Net | HTML Probe | .Net Probe
   
 www.solvingera.com 
  Contact Us |Sitemap
  
 

Caching in Asp.Net

 Skip Navigation LinksHome>Asp Home>Caching
Caching is a technique in which data is stored temporarily either on a web server or on the client side for later retrieval. In Asp.Net the Web server, on which the application resides, processes the requests for the website only one time and caches the request for future. So we can say that caching is the simplest way to improve the processing time of a web application.

This concept reduces the load on the database server as well as reduces the time users have to wait to access the specific data. When caching is used in Web application, less bandwidth is required because there is no need to travel the Webpage request or responses between the client and the server.

Types of Caching:

  • Output Caching: Output Caching is a way to store the page output and Hypertext Transfer Protocol response in the server cache or disk cache which stores the cache data in RAM. Output Caching also known as page level caching. In this caching, the output of the user requested page comes from the cached memory instead of re-rendering the page. This caching is also very useful for a Website which contains static pages.

    You can easily implement output caching by inserting the @OutputCache directive at the top of a .aspx page, as shown in the below code snippet:

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

    Some important attribute of @OutputCache are given below:
    • Duration: It defines the time duration in seconds for a page to be stored in the cache.

    • CacheProfile : It contains the setting of the cache to associate with a Web page. By default it contains an empty string.

    • Location: This attribute defines the location where the cache is created. The different values of location attribute are Any, Client, Server, None, ServerAndClient and Downstream.

    • VaryByHeader: Defines a semicolon-separated list or HTTP headers.

    • VaryByParam: This attribute determines that how many copies of the page output are actually cached.

    • VaryByCustom: This attribute is used to define custom string according to the requirements of a user.


  • Partial-Page Output Caching: Partial-Page Output Caching is a technique in which the data from specific section of the page is cached and the remaining content of the web page is refreshed each time. This type of caching is also called Fragment Caching. To implement fragment caching, you need to create a user control for the portion of the page you want to cache. You can then add the OutputCache directive to the user control. The result is that the page will not be cached, but the user control will.

  • Data Caching: Data caching is the most flexible type of caching, but it also forces you to take specific additional steps in your code to implement it. Data caching is used to cache custom data into a special built-in collection object called Cache. The cache object works like the Application object. This object is globally available to all requests from all clients in the application.

    However a few key differences between cache object and Application object are given below:

    • There is no need to use LockorUnlock method in cache object.

    • Items in the cache are removed automatically.

Cache Dependencies

Cache dependencies allow a developer to make a cached item dependent on another resource so that when that resource changes, the cached item is removed automatically. Some dependency models are given below:
  • File dependency: File dependency allows a developer to validate a cached item with external file such as XML. Cached items are depends on this external file and this items will removed automatically if there is any change in that external file.

  • Key dependency: If one cached item is updated or removed, the dependent cached item also perform the same task (updated or removed).

  • Time dependency: Sets the expiration time of a particular item of a database.

Adding/retrieving/deleting items in Cache

A code snippet given below shows how to add an item in cache:

cache("VariableName") = "ValueName";

Adding a item using Cache.Insert method :

Cache.Insert("VariableName", "ValueName");

A code snippet given below shows how to Retrieving an item from cache:

Lable1.Text = Cache["VariableName"];

A code snippet given below shows how to Delete an item from cache:

Cache.Remove["VariableName"];
  
     
     
  
DMCA.com Protection Status
Design & Maintain By solvingera group| solvingera.com © 2013-2014 | all right reserved |Privacy Policy | Back to top