• Caching in ASP.NET

    The ability to store data in the main memory and then allow for retrieval of the same as and when they are requested.

    Caching is a technique of persisting the data in memory for immediate access to requesting program calls.

     

    ASP.NET supports three types of caching for Web-based applications:

     

     

    1. Page Level Caching (called Output Caching)

    2. Page Fragment Caching (often called Partial-Page Output Caching)

    3. Programmatic or Data Caching

     

    Output Caching

     

    Output caching caches the output of a page (or portions of it) so that a page's content need not be generated every time it is loaded.

     

    In a typical ASP.NET page, every time the user views the page, the Web server will have to dynamically generate the content of the page and perform the relevant database queries (which is a very expensive task to do).

     

    Considering the fact that the page does not change for a certain period of time, it is always a good idea to cache whatever is non-static so that the page can be loaded quickly.

     

    In Page Output Caching, the entire page is cached in memory so all the subsequent requests for the same page are addressed from the cache itself.

     

    In Page Fragment Caching, a specific a portion of the page is cached and not the entire page. Page Output or Fragment Caching can be enabled or disabled at the Page, Application or even the Machine levels.

     

    Data Caching allows us to cache frequently used data and then retrieve the same data from the cache as and when it is needed. We can also set dependencies so that the data in the cache gets refreshed whenever there is a change in the external data store. The external data store can be a file or even a database. Accordingly, there are two types to dependencies, namely, file based and Sql Server based. There are also differing cache expiration policies

     

    Syntax: <%@ OutputCache Duration="60" VaryByParam="none" %>

     

    The above syntax specifies that the page be cached for duration of 60 seconds and the value "none" for VaryByParam* attribute makes sure that there is a single cached page available for this duration specified.

     

    * VaryByParam can take various "key" parameter names in query string. Also there are other attributes like VaryByHeader, VaryByCustom etc.

     

     

    The following is the complete syntax of page output caching directive in ASP.NET.

     

    <%@ OutputCache Duration="no of seconds"

    Location="Any | Client | Downstream | Server | None"

    VaryByControl="control"

    VaryByCustom="browser |customstring"

    VaryByHeader="headers"

    VaryByParam="parameter" %>

     

     

    To store the output cache for a specified duration

     

    Declarative Approach:

     

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

     

    Programmatic Approach:

     

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

    Response.Cache.SetCacheability(HttpCacheability.Public);

     

    To store the output cache on the browser client where the request originated

     

    Declarative Approach:

     

    <%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %>

     

    Programmatic Approach:

     

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

    Response.Cache.SetCacheability(HttpCacheability.Private);

     

    To store the output cache on any HTTP 1.1 cache-capable devices including the proxy servers and the client that made request

     

    Declarative Approach:

     

    <%@ OutputCache Duration="60" Location="Downstream" VaryByParam="None" %>

     

    Programmatic Approach:

     

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

    Response.Cache.SetCacheability(HttpCacheability.Public);

    Response.Cache.SetNoServerCaching();

     

    To store the output cache on the Web server

     

    Declarative Approach:

     

    <%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>

     

    Programmatic Approach:

     

    TimeSpan freshness = new TimeSpan(0,0,0,60);

    DateTime now = DateTime.Now;

    Response.Cache.SetExpires(now.Add(freshness));

    Response.Cache.SetMaxAge(freshness);

    Response.Cache.SetCacheability(HttpCacheability.Server);

    Response.Cache.SetValidUntilExpires(true);

     

    To cache the output for each HTTP request that arrives with a different City:

     

    Declarative Approach:

     

    <%@ OutputCache duration="60" varybyparam="City" %>

     

    Programmatic Approach:

     

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));

    Response.Cache.SetCacheability(HttpCacheability.Public);

    Response.Cache.VaryByParams["City"] = true;

     

    For the VaryByCustom attribute, the VaryByHeader attribute, and the VaryByParam attribute in the @ OutputCache directive, the HttpCachePolicy class provides the VaryByHeaders property and the VaryByParams

    property, and the SetVaryByCustom method.

     

    1. Page Output Caching

     

    In Page Output Caching, the entire page is cached in memory so all the subsequent requests for the same page are addressed from the cache itself.

     

    Partial-Page Output Caching/ Page Fragment Caching

     

    In Page Fragment Caching, a specific a portion of the page is cached and not the entire page. Page Output or Fragment Caching can be enabled or disabled at the Page, Application or even the Machine levels.

     

    More often than not, it is impractical to cache entire pages. For example, you may have some content on your page that is fairly static, such as a listing of current inventory, but you may have other information, such as the user's shopping cart, or the current stock price of the company, that you wish to not be cached at all. Since Output Caching caches the HTML of the entire ASP.NET Web page, clearly Output Caching cannot be used for these scenarios: enter Partial-Page Output Caching.

     

    Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to be cached.

     

    fragment caching comes from the attribute "VaryByControl". Using this attribute one can cache a user control based on the properties exposed.

     

    Syntax: <%@ OutputCache Duration="60" VaryByControl="DepartmentId" %>

     

    The above syntax when declared within an *.ascx file ensures that the control is cached for 60 seconds and the number of representations of cached control is dependant on the property "DepartmentId" declared in the control.

     

    Data Caching

     

    Programmatic or data caching takes advantage of the .NET Runtime cache engine to store any data or object between responses. That is, you can store objects into a cache, similar to the storing of objects in Application scope in classic ASP.

     

    Realize that this data cache is kept in memory and "lives" as long as the host application does. In other words, when the ASP.NET application using data caching is restarted, the cache is destroyed and recreated. Data Caching is almost as easy to use as Output Caching or Fragment caching: you simply interact with it as you would any simple dictionary object.

     

    Note that the Insert method allows you to simply add items to the cache using a key and value notation as well. For example to simply add an instance of the object bar to the cache named foo, use syntax like this:

     

    Cache.Insert("foo", bar); // C#

    Cache.Insert("foo", bar) ' VB.NET

  • Interview questions

    ASP.NET 3.5 Interview Questions

    Dear Friends,

    My aim to provide a
    Complete Material of DOT NET Interview questions including ASP.NET,ADO.NET,Visual Source safe, DOT NET Framework,OOPS. this material will be very helpful to you in interview.

    Visit http://aspnet2008.blogspot.com/

    ***********

    ASP.NET AJAX Interview Questions

    Dear Friends,

    My aim to provide a Complete Material of DOT NET AJAX including AJAX problem on remote server,AJAX tutorial,AJAX Interview question,Free ebooks of AJAX and much more

    Visit http://ajaxsample.blogspot.com
    ***********
    Free IT Books, Free Java books, Free J2EE books, Free .NET books

    Dear Friends,

    You are free to download ebooks of AJAX, ASP.NET, VB.NET, SQL Server, PHP, JAVA, software, oracle, Unix, Google Adsense, Linus, SAP, Pearl and much more.

    Download from http://freedownload12.blogspot.com/

    **********

    fix programming error

    Dear Friends,

    get solution of all type of programming bugs in AJAX, VS.NET,ASP.NET, PHP, Sql server and much more

    Click below link to get solution.

    http://bugtrace.blogspot.com

    ************

    Sql server Interview Questions And answers

    Dear members,

    we provide a solution of bugs related to sql server along with study material and interview preparation of SQL server.Complete set of interview question of sql server with stored procedure,functions,triggers,query,DBMS including with free download ebook and much more

    click below link to get your solution

    http://mssqlinterview.blogspot.com/

    ********

    javascript Interview Questions

    Dear Friends,

    My aim to provide a Complete Material of Javascript interview questions and answers with study material including lot of script code and much more.

    click below link to get your solution

    http://javascriptinterview.blogspot.com

Footer:

The content of this website belongs to a private person, blog.co.uk is not responsible for the content of this website.