Pages

Tuesday, June 21, 2011

Caching

What is Caching?
      - It is a technique used to store the data and web pages temporarily in the main memory, which could be used later.
     - As it minimizes the usage of server resources, it improves the performance of the application.

Why Caching?
     - Fetching data form the database is one of the slowest web site operations.
     - Each time the request is made, it undergoes "Client -> Server -> Database -> Server -> Client" cycle. This is very time consuming. And depending upon the data density, the time taken would increase.
     - This process affects the performance much, when many people access simultaneously.
     - Here when Caching concept is applied, we could improve the performance.
     - By using caching the database data could be cached in memory and could avoid the database access with every request.

Types of Caching:
     - Page Level Output Caching.
     - Data Caching
     - Fragment Caching.

Page Level Output Caching:
    - Mainly used for static pages.
    - Advantages - Simple to implement, and are sufficient in many cases.
    - It catches the output of the page and gets stored. This avoid the generation of page for each request.
    - It just stores a copy of HTML in memory, that was sent to response on a request.
    - To implement add OutputCache directive on to the page, which needs to be cached.
         Code Snippet:
        &lt%@ Page Language ................... % &gt
        &lt@ OutputCache Duration="10" VarByParam="none" %&gt
    - Here this page would be cached for 10 sec
    - when the cache is refreshed repeatedly this time remains constant until the cache is expired.

Page Fragment Caching:
    - It stores only the part of the page to be cached.
    - Ex, when the user logs in, his user name is displayed in header till he logs out. thus this header portion alone could be cached using this technique. For this the header need to be defined in user control.
    - To implement, add OutputCahce directive in the user control page that has to be cached.
         &lt%@ OutputCache Duration="30" VarByControl="Header"  %&gt
    - Here this header user control would be cached for 30 sec.

Data Caching / Application Caching:
    - It is much more powerful than the other two.
    - Here a part of the data is been cached and stored as object. Its scope is that of the application.
    - It is done using "cache" class. Here an item is cached under a name and retrieve it with the same name when required.
    - Example:
           For storing ---  Cache["ID"] = EmployeeID
           For Retrieving - EmployeeID = Cache["ID"]

Where does the Cache data gets stored??
    - The location of the cached data could be specified in the OutputCache directive.
    Any:
         - This stores the output cache in the client's browser, on proxy server (or any other server) that participates in the request.
         - By default Any is selected.
    Client Caching:
         - This stores the output cache in client's browser.
    Downstream:
         - This stores in any cache capable device that participates in page request.
    Web Server Caching:
         - This stores the output cache in the web server.
    None:
         - Output caching is deactivated.

No comments:

Post a Comment