Data Caching in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Sep 16, 2015.

  1. ASP.NET run time contains a key value map known as cache. The System.Web.UI.Page and HttpContext are used. The state of data caching is non deterministic.

    Caching in ASP.NET

    In ASP.NET, various ways to cache the data are provided by the user. They are as mentioned below:
    1. Data Caching: The data is cached from the data source. Once the data is expired, new data is obtained from the data source and added in the cache.
    2. Output Caching: The copy of rendered HTML pages is added to the cache. Once there is a request for the specific page, it is accessed from the cache.
    3. Class Caching: The assembly contains the web pages and services. When the request for the web page is made, the cached assembly is referred.
    4. Object Caching: The objects are cached on a page. The cached data is saved inside the memory.
    5. Configuration Caching: The configuration file contains the configuration data. Inside the server memory, data is saved.
    Output Caching

    The performance of ASP.NET application is enhanced by caching the ASP.NET markup. The round trips to the web server are avoided. If the page content is same most of the times, output caching is useful.

    The @OutputCache directive is used for output caching. The syntax for the directive is:

    Code:
    <%@ OutputCache Duration = "10" VarByParam ="None" %>
    
    Attributes used in Output Caching
    1. VarByParam: The list of string separated by a semicolon defines the query string values in a GET request or variable in a POST request
    2. VarByHeader: The list of string separated by semicolon the headers provided by the client.
    3. CacheProfile: The profile used in the web.config file
    4. NoStore: The no store cache control is specified
    5. DiskCacheable: Output to be written to the disk based cache
    6. Location: The location for caching can be client, browser, server, downstream or none.
    7. Duration: The time in seconds defined for the page control to be cached
    To add the Output Cache, below the @Page directive, add the following code.

    Code:
    <%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Default.aspx.cs" Inherits = "_Default" %>
    <%@ OutputCache Duration = "10" VarByParam = "None" %>
    
    Data Caching

    The data highly used by the programmers is placed in a cache. The Page class property returns an instance of System.Web.Caching.Cache class. The object of cache can be used by all the clients.

    The object of cache is not explicitly locked or unlocked. The items are automatically removed from the cache object. The items can be linked to a database table, file, or any resource.

    User can programmatically implement the data cache. The Cache class encapsulates the data caching and the Cache class object. The items are saved in key – value pair.

    Consider an example; the variable name is saved in the Cache object. The value is assigned in the code shown below:

    Code:
    Cache [ "name" ] = "Sam";
    
    The value can be retrieved using the Cache object as shown below.

    Code:
    TextBox1.Text = Cache [ "name" ];
    
    Object Caching

    The object caching provides user with the ease to use. Any object can be placed in the cache. The object can be a web control, data type, a class, a dataset object, etc.

    Code:
    Cache [ "key" ] = item;
    
    ASP.NET provides user with Insert() method used for adding objects inside the cache. The different overloaded versions of the method are:
    1. Cache.Insert ( key, value, dependencies ): The item is added to the cache using the default priority, CacheDependency names and links.
    2. Cache.Insert ( key, value ): An item inside the cache with key name and value consisting default priority is added
    3. Cache.Insert ( key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallBack): The cache priority can be set for the cache item and a delegate pointing to the invoked method.
    4. Cache.Insert ( key, value, dependencies, absoluteExpiration, slidingExpiration): The expiration policy along with some of the above mentioned issues.
    The following code snippet demonstrates the sliding expiration for 5 minutes.
    Code:
    Cache.Insert ( "item1", obj, null, DateTime.MaxValue, TimeSpan.FromMinutes ( 15) );
    
    Consider an example for demonstrating the use of object caching
    Code:
    protected void Page_Load( object sender, EventArgs e)
    {
        if ( this.IsPostBack )
        {
            label1.Text + = "Page is posted back";
        }
        else
        {
            label1.Text + = "Page is created";
        }
        
        if ( Cache [ "item"] == null )
        {
            label1.Text + = "New item is created";
            DateTime item = DateTime.Now;
            label1.Text + = "Item is stored";
            Cache.Insert ( "item", item, null );
            DateTime.Now.AddSeconds ( 20 ), TimeSpan.Zero;
        }
    
        else
        {
            label1.Text + = "Item is accesses";
            DateTime item = ( DateTime) Cache [ "item" ];
            label1.Text + = "Time is: " + item.ToString();
            label1.Text + = <br/>";
        }
        
        label1.Text + = "<br/>";
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice