• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Aspnet Caching
 

Aspnet Caching

on

  • 3,833 views

An Introduction to caching in Asp.net

An Introduction to caching in Asp.net

Statistics

Views

Total Views
3,833
Views on SlideShare
3,828
Embed Views
5

Actions

Likes
2
Downloads
124
Comments
2

2 Embeds 5

http://www.linkedin.com 4
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • This is nice slide, very helpful for me. This link...
    http://www.mindstick.com/Interview/1253/What%20is%20Fragment%20Caching%20in%20ASP%20NET
    also helpful.
    Are you sure you want to
    Your message goes here
    Processing…
  • well explained presentation, there is no doubt that asp.net cache is the ultimate solution for the issues like performance and scalability. But before going ahead with it, one has to keep into mind its drawbacks/limitations as well. due to its standalone and in-process nature, it only performance well in smaller web farms. But in a larger web garden, where you have multiple servers, the use of a distributed cache can be a better option. here is a good read about the limitations of asp.net cache,

    http://www.alachisoft.com/ncache/asp-net-cache.html
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Aspnet Caching Aspnet Caching Presentation Transcript

      • http://www.linkedin.com/in/thisisshubho
    • Overview
      • Introduction to ASP.NET caching
      • Output caching
      • Fragment caching
      • Data caching
    • Introduction to Caching in ASP.NET
      • Caching is the most critical factor in creating scalable, high performance Web applications
      • Caching locations
        • Web server, proxy server, and client browsers
      • Types of caching
        • Output caching
        • Fragment caching
        • Data caching
    • Output Caching
      • What is output caching?
      • @ OutputCache directive and the cache object
      • Output caching attributes:
        • Duration
        • Location
        • VaryByParam
        • VaryByHeader
        • VaryByCustom
    • What Is Output Caching?
      • Pages that use the output cache are executed one time, and the page results are cached
      • The pre-executed page is then served to later requests
      • Performance and scalability both benefit
        • Server response times reduced
        • CPU load reduced
      • Appropriate caching of pages affects site performance dramatically
    • @ OutputCache Directive and the Cache Object
      • @ OutputCache declaratively controls caching behavior
        • For .aspx, .asmx, or .ascx
      • The cache object programmatically controls caching behavior
      <%@ OutputCache Duration=&quot;600“ Location=&quot;Any“ VaryByParm=“none” %> Is equivalent to: [C#] Response.Cache.SetExpires(DateTime.Now.AddSeconds(600)); Response.Cache.SetCacheability(HttpCacheability.Public);
    • OutputCache Members: Duration and Location
      • Duration sets the time to cache the output
        • In seconds
        • Required
      • Location sets the location to cache the output
        • Server : The output is held in memory on the Web server and is used to satisfy requests
        • Downstream : A header is added to the response to indicate to proxy servers to cache the page
        • Client : A header is added to the response indicating to browsers to cache the page
        • Any : Output cache can be located on any of these locations
        • None : No output caching is turned on for the item
      <%@ OutputCache Duration=&quot;600&quot; Location=&quot;Any“ VaryByParam=“none” %>
    • OutputCache Members: VaryByParam and VaryByHeader
      • VaryByParam
        • The cache stores multiple copies of a page based on specific Querystring or Form parameters and any combinations thereof
      • VaryByHeader
        • The cache stores multiple copies of a page based on HTTP headers
      <%@ OutputCache Duration=&quot;10“ VaryByParam=&quot;location;count&quot; %> <%@ OutputCache Duration=&quot;60“ VaryByHeader=&quot;Accept-Language&quot; %>
    • OutputCache Members: VaryByCustom
      • VaryByCustom
        • If the value is “Browser,” cache varies by browser type and major version
        • If the value is a custom string, you must override HttpApplication.GetVaryByCustomString in the Global.asax and implement your own caching logic
    • Fragment Caching
      • What is fragment caching?
      • VaryByControl
      • Nested cached user controls
      • Cached controls are not programmable
    • What Is Fragment Caching?
      • Just as you can vary the versions of a page that are output cached, you can output cache regions of a page
      • Regions are defined based on user controls
      • User controls contain their own @OutputCache directive
      • Fragment caching supports
        • VaryByParam
        • VaryByControl
      • Location not supported because fragments must reside on server to be assembled
    • Fragment Caching a User Control
      • [*.ascx]
      • <%@ Language=&quot;C#&quot; %>
      • <%@ OutputCache Duration=&quot;10“ VaryByControl=&quot;State;Country&quot;
      • VaryByParam=&quot;*&quot;%>
      • <script runat=server>
      • public String State {
      • get { return state.Value; }
      • set { state.Value = State; } }
      • public String Country {
      • get { return country.Value; }
      • set { country.Value = Country; } }
      • </script>
    • VaryByControl
      • VaryByControl
        • The sixth attribute supported by OutputCache
        • Only supported in user control caching
        • Caching is based on user control properties
      <%@ OutputCache Duration=&quot;10“ VaryByControl=&quot;State;Country“ VaryByParam=&quot;*&quot;%>
    • Post cache substitution
      • Allows to substitute (Replace/modify) a (Small) portion of a cached page output
      • Very useful to use where little content in the rendered page varies depending on parameter(s)
      • Use an asp:substitution to implement the Post cache substitute
      • Provide a method for the call back, that renders the markup for the substitution.
    • SQL dependency
      • Output Caching/ Fragment caching could be configured to use SQL dependency so that, cached Page/User control will be updated when corresponding table(s) data is/are updated
      • SQL dependency is usable in Both SQL server 2000 and SQL server 2005
    • SQL dependency
      • The “Polling model” has to be used in SQL server 2000 to use Cache dependency
      • --Application “Polls” data after a given period of time
      • --Requires more effort in configuring
      • --More resource intensive
      • The “Notification model” can be used in SQL server 2005 to use Cache dependency
      • --Requires less effort in configuring
      • --DB server “Notifies” application with Updates
      • --Less resource intensive
    • SQL dependency:Example
      • In ASPX, or, ASCX page, use
      • <asp:SqlDataSource EnableCaching=&quot;True&quot; SqlCacheDependency=&quot;Northwind:Products&quot; ... />
      • In web.config, use the following configuration:
      • <caching>
      • <sqlCacheDependency enabled=&quot;true“ pollTime=&quot;1000&quot;>
      • <databases>
      • <add name=&quot;Northwind“ connectionStringName=&quot;NorthwindConnectionString1&quot;/>
      • </databases>
      • </sqlCacheDependency>
      • </caching>
      • Enable SQL server for SQL dependency: Execute following command in the command prompt
      • aspnet_regsql.exe -S %Server% -U %uid% -P %pwd% -d %Database% -t %TableName% -et
    • Page/Fragment output cache : Best practices
      • Don’ts
      • Generally use of output cache in pages that contain input forms is not a good idea.
      • Generally, it is good practice to not to use output caching in pages that requires authentication.
      • Don’t use output cache for static page.
      • Don’t use output cache for page that contains lighter content
      • Don’t use output cache for page that generally executes faster
    • Page/Fragment output cache : Best practices
      • Do’s
      • Use output cache in public pages
      • Use output cache for pages that executes slower
      • Use output cache for pages that contains heavy contents
      • Use output cache for pages that are frequently being accessed.
      • Use Fragment cache (Output cache at User controls) to cache varying common dynamic contents across the pages
    • Page/Fragment output cache : Best practices
      • Do’s
      • Use Post cache substitution for fewer contents in the pages that vary across different parameters
      • Use SQL dependency (With longer durations) with output cache for pages that uses data that are very frequently accessed, expensive to retrieve and that are not changed often.
      • Set smaller durations for output cache in pages that varies too much depending on input parameters, and/or, that are not frequently accessed
    • Page/Fragment output cache : Best practices
      • Do’s
      • Use moderate duration for pages that are less
      • frequently accessed and/or that doesn’t vary too much with input parameters
      • Use long duration for pages that are very frequently accessed and/or that vary little with input parameters
      • Use Output cache profiles for better manageability (Configuring profiles in web.config)
    • Data Caching
      • What is data caching?
      • Working with the cache object
      • Cache dependencies
      • Scavenging memory
      • Using callbacks with caching
    • What Is Data Caching?
      • The data cache holds application data such as strings, datasets, and other objects
      • Adding items to the data cache is easy
      • Although similar to the familiar application variables model, it is much more powerful
      Cache [“counter”] = mycount.text Application[“counter”] = mycount.text
    • Working with the Cache Object
      • Cache object features
        • Dependencies allow logic to invalidate cached items
        • Scavenging (automatic expiration)
        • Callbacks when an item is removed from cache
      • To use dependencies or callbacks, use Cache.Insert or Cache.Add
      • Code using cached items must be able to both create or insert, and retrieve cached items
      Public DataSet GetProductData() { if (Cache[&quot;ProductData“] = null) { Cache[&quot;ProductData“] = LoadDataSet(); }   Return Cache[&quot;ProductData“]; }
    • Cache Dependencies
      • File-based dependencies
        • Cached item invalidated when files change
      • Key-based dependencies
        • Cached item invalided when another cached item changes
      • Time-based dependencies
        • Absolute time-based invalidations
        • Sliding time-based invalidations
      • SQL dependencies
        • SQL based invalidations
    • Scavenging Memory
      • Automatic system that initiates when memory becomes scarce
      • Tag cached items with relative importance
        • CacheItemPriority
        • CacheItemPriorityDecay
      • Items must be added to cache using .Add or .Insert to make use of these enumerations
    • Using Callbacks with Caching
      • Create Callbacks with this delegate:
      • CacheItemRemovedCallback
      • Callbacks are used to receive notification when an item is evicted from the cache
        • Cleanup
        • Update
        • Recreate
        • Logging
      • Items must be added to caching using .Add or .Insert to use callback functionality
    • Caching in Web Farm
      • ASP.NET cache is not usable in a web farm scenario (Load-balanced distributed deployment).
      • The .NET remoting could be used to develop a cache server, or, third party caching provider can be used (Ncache, Distributed Cache etc)
    • Review
      • Code that uses items from the data cache must be able to create the object, load the cache, or retrieve the object before using it.
      • Caching can hide problems. Test without caching.
      • Use tracing, performance counters, and stress software to identify caching wins.
    • More information
      • aspnet .4guysfromrolla.com/articles/022802-1. asp x
      • msdn.microsoft.com/en-us/library/ms972362.aspx