Caching
A well-designed caching strategy is probably the single most important performance-related design consideration. ASP.NET caching features include output caching, partial page caching, and the cache API. Design your application to take advantage of these features.
Caching can be used to reduce the cost of data access and rendering output. Knowing how your pages use or render data enables you to design efficient caching strategies. Caching is particularly useful when your Web application constantly relies on data from remote resources such as databases, Web services, remote application servers, and other remote resources. Applications that are database intensive may benefit from caching by reducing the load on the database and by increasing the throughput of the application. As a general rule, if caching is cheaper than the equivalent processing, you should use caching. Consider the following when you design for caching:
-
Identify data or output that is expensive to create or retrieve. Caching data or output that is expensive to create or retrieve can reduce the costs of obtaining the data. Caching the data reduces the load on your database server.
-
Evaluate the volatility. For caching to be effective, the data or output should be static or infrequently modified. Lists of countries, states, or zip codes are some simple examples of the type of data that you might want to cache. Data or output that changes frequently is usually less suited to caching but can be manageable, depending upon the need. Caching user data is typically only recommended when you use specialized caches, such as the ASP.NET session state store.
-
Evaluate the frequency of use. Caching data or output that is frequently used can provide significant performance and scalability benefits. You can obtain performance and scalability benefits when you cache static or frequently modified data and output alike. For example, frequently used, expensive data that is modified on a periodic basis may still provide large performance and scalability improvements when managed correctly. If the data is used more often than it is updated, the data is a candidate for caching.
-
Separate volatile data from nonvolatile data. Design user controls to encapsulate static content such as navigational aids or help systems, and keep them separate from more volatile data. This permits them to be cached. Caching this data decreases the load on your server.
-
Choose the right caching mechanism. There are many different ways to cache data. Depending on your scenario, some are better than others. User-specific data is typically stored in the Session object. Static pages and some types of dynamic pages such as non-personalized pages that are served to large user sets can be cached by using the ASP.NET output cache and response caching. Static content in pages can be cached by using a combination of the output cache and user controls. The ASP.NET caching features provide a built-in mechanism to update the cache. Application state, session state, and other caching means do not provide a built-in mechanism to update the cache.
Static content
setting your content to be cached on the user browser
this is good for Static content like UI images
i want your browser to cache the content in this path where is Jquery images and css, 10 days Duration.
<%@ OutputCache Location="???
"Client" - The output cache is located on the browser client.
"Server" - The output cache is located on the Web server.
"Any" - The server,a proxy,client.
"Downstream" - Not in the server.
<%@ OutputCache VaryByParam="???
how the caching should be performed according to the query string supplied
"none" - cache the page regardless of query string.
"*" - cache the page regardless of what is the query string.
"the queryname param" - ?param=12&id=13 will cache a page with this syntax , a id=14 will show the same page from the cache.
Dynamic content
In general, dynamic content should have an expiration time of between 1 and 30 days, depending on the
details of your application. An example of doing that declaratively is to place an OutputCache directive at
the top of your .aspx page:
<%@ Page . . . %>
<%@ OutputCache Duration="86400" Location="Client" VaryByParam="None" %>
That tells the runtime to generate HTTP headers that ask the browser to cache the page for 86,400
seconds (one day). You must include VaryByParam, or the parser will generate an error. A value of None
means that multiple versions of the page do not need to be cached independently. The resulting HTTP
headers are as follows:
Cache-Control: private, max-age=86400
Expires: Tue, 17 Jan 2010 01:34:17 GMT
Substitution Caching
in short imaging a User Control that is cached and only his content is dynamic.
here is a page refresh it every 5 sec to see the change.
cache declering
<%@ OutputCache Duration="5" VaryByParam="None" %>
in the body
Cached time:<%= DateTime.Now.ToString() %>
<br />Page time :
<asp:Substitution ID="sub" runat="server" MethodName="SubTime" />
<br />Random GUID :
<asp:Substitution ID="MySub" runat="server" MethodName="GUID" />
cs code:
protected void Page_Load(object sender, EventArgs e){}
public static string SubTime(HttpContext context)
{return DateTime.Now.ToString();}
public static string GUID(HttpContext context)
{return Guid.NewGuid().ToString();}