|
|
|
Summary
In the previous sections, you learned the
different methods of storing state information in ASP.NET. Although
all the methods are relatively simple and basically perform the
same functions, all the
|
|
|
|
|
|
|
Further Reading
Programming Windows with C# , by Charles Petzold , Microsoft Press. ISBN: 0735613702. ASP.NET Unleashed , Second Edition, by Stephen Walther , Sams Publishing. ISBN: 067232542X. |
|
|
|
|
|
|
Chapter 24. CachingIN BRIEF This chapter introduces you to the world of data caching in ASP.NET. You will learn the different techniques of caching data in ASP.NET and when you should use each of the different means of caching. Next, you will learn about the OutputCache directive and how to use it. Finally, you will learn about the HttpCachePolicy and Cache objects. WHAT YOU NEED
CACHING AT A GLANCE
|
|||||||||||||||||||||||||||||||
|
|
|
|
|
|
CachingDevelopers often need to temporarily put data (temporary data) in a storage medium (for example, a database or some location in memory) for quick access. The storage of this temporary data is called a cache .
A cache provides a number of possibilities. For
example, imagine that you're working with a database in which data
will be changed very rarely. When a
All cached data is stored in memory so that it
can be retrieved and
ASP.NET
Introduction to ASP.NET CachingThe code in Listing 24.1 is a simple example that shows how to use a cache in ASP.NET. Listing 24.1. Caching HTML Pages by Using the OutputCache Directive
<%@ Page language="c#"
Codebehind="Main.aspx.cs"
AutoEventWireup="false" Inherits="OutputCacheSample.Main" %>
<%@ OutputCache Duration="60" VaryByParam="none"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>OutputCache using Sample</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_
targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
Page's invocation time:
</td>
<td>
<%=DateTime.Now.ToString("F")%>
</td>
</tr>
</table>
</body>
</html>
This example simply outputs the server's time on a web page. When the page first appears, it is cached because of the OutputCache directive in the second line of Listing 24.1. The page will be stored in the cache for 60 seconds. If a user requests this page, his browser will show the cached page with the old time. After the cache has been cleared, the user can view the updated page. We have two ways to cache HTML pages:
OutputCache and HttpCachePolicy are very similar, but using OutputCache is more user friendly. The following section discusses how to use the OutputCache directive in detail. OutputCache DirectiveListing 24.1 showed how to use the OutputCache directive to quickly retrieve HTML pages from the cache. That example is very simple because it uses just one attribute from the available set of predefined parameters of the OutputCache directive. It uses the parameter Duration , which declares the time (in seconds) that pages should be stored in the cache. The example also declares that the page will have just one cached version, independent of the set of parameters of this one. The OutputCache directive has the following attributes: <%@ OutputCache Duration="value" Location="value" VaryByParam="value" VaryByHeader="value" VaryByCustom="value" %> The following list describes each attribute in more detail:
To use the Location attribute in OutputCache , you should change the second line in Listing 24.1 to the following: <%@ OutputCache Duration="60" VaryByParam="none" Location="Server"%> In this case, the page will be cached for 60 seconds only on the server side.
TIP
Note that different values of the
Location
attribute will cause different behavior by the
application. You should use it very
Using HttpCachePolicyAs mentioned earlier, ASP.NET provides two ways to cache HTML pages. The first approachthe OutputCache directivewas discussed earlier. So, the following section covers the second approach: the HttpCachePolicy class. To be more precise, the OutputCache directive is just an intuitive interface of the HttpCachePolicy class. But OutputCache gives more limited possibilities than HttpCachePolicy . For example, you aren't allowed to set an absolute expiration time for storing an object in the cache with the help of OutputCache , but HttpCachePolicy allows you to do so. You could retrieve access to that object via the Response.Cache object represented in ASP.NET. Listing 24.2 is an example that shows the simplest way of using HttpCachePolicy . (The ASPX representation of this example is the same as in Listing 24.1, but without the use of the OutputCache directive.) Listing 24.2. Caching HTML Pages by Using the HttpCachePolicy Class
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace HttpCachePolicySample
{
public class Main : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
This example caches the page by using two
TIP
Note that this article doesn't describe all the
The SetExpires method helps to set an absolute expiration time for a page in the cache. The example in Listing 24.2 shows how to use SetExpires in tandem with the DateTime class. You are also allowed to use it in the following way:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetExpires(DateTime.Parse("04:00"));
In this case, the page will be deleted from the cache at 4 o'clock local time. Both methods that have been discussed enable you to set an absolute expiration time. But there is one more ASP.NET method that enables you to set the caching type to a sliding mode: Response.Cache.SetSlidingExpiration( true );
When you use absolute caching, the page is
stored in the cache until a particular amount of time
Using the Cache ObjectThere is another class in ASP.NET that enables you to cache data. This class is called Cache , and you get access to it via the Cache property of the current context of the web application (for example, Context.Cache ). This object enables you to both store data and to retrieve stored data. It also supports the following:
Listing 24.3 shows how to use the Cache object. This example puts some data into the cache and shows that data in the browser. It also checks the source from which the data has been retrieved. Listing 24.3. Using the Cache Object (Code-Behind)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CacheSample
{
public class Main : System.Web.UI.Page
{
protected HtmlTableCell DataCell;
protected HtmlTableCell SourceCell;
private void Page_Load(object sender, System.EventArgs e)
{
String data = "Some data that should be cached.";
String source = "";
if (Cache[SOME_DATA_KEY] != null)
{
data = Cache[SOME_DATA_KEY].ToString();
source = "Cache";
}
else
{
Cache[SOME_DATA_KEY] = data;
source = "Not Cache";
}
DataCell.InnerText = data;
SourceCell.InnerText = source;
}
private const string SOME_DATA_KEY = "SOME_DATA_KEY";
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Listing 24.4 shows the HTML version of this example. Listing 24.4. Using Cache Object (HTML Representation)
<%@ Page language="c#" Codebehind="Main.aspx.cs"
AutoEventWireup="false" Inherits="CacheSample.Main" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Cache using Sample</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<table width="100%" cellpadding="0" cellspasing="0" border="0">
<tr>
<td width="20%">Data: </td>
<td runat="server" id="DataCell"></td>
</tr>
<tr>
<td>Source: </td>
<td runat="server" id="SourceCell"></td>
</tr>
</table>
</body>
</html>
After the first request to the page shown in Listings 24.3 and 24.4, you will see that the source of the data is Not Cache . All other requests (until the page is deleted from the cache) will return that data retrieved from the cache.
TIP
Note that it is mandatory to check whether an
object is located in the cache;
The preceding example used an implicit approach
to
Cache.Insert("SOME_DATA_KEY", data);
The Insert method also has three additional overloads that are discussed in the upcoming sections. Assume that you've read data from a file and put it in the cache. There is some chance that file could be changed, so data in the cache also should be updated or deleted. The Cache object enables you to trace all changes in a file with data by using an additional parameter (in Cache.Insert method) with type System.Web.Caching.CacheDependency . To use this feature, you create an instance of the CacheDependency class and pass it to the Cache.Insert method: System.Web.Caching.CacheDependency dependency = new System.Web.Caching.Dependency(Server.MapPath(someFileName)); Cache.Insert(SOME_DATA_KEY, data, dependency); Before returning an object, Cache traces changes in the file that the object depends on. If the file has changed, the object is removed from Cache ; otherwise, it is returned to Cache 's user. The System.Web.Caching.CacheDependency class has overloads for the constructor, which enables you to trace the changes in the following:
TIP Unfortunately, due to the transient nature of instances of Page classes, there is no built-in way in ASP.NET to create a dependency that removes an item from the cache in response to changes in a DataSet. Another feature is that Cache enables you to set absolute and sliding expiration times for objects that should be inserted in it. (The differences between absolute and sliding time were discussed earlier in this chapter.)
Cache.Insert("SOME_DATA_KEY", data, null, DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration);
Cache.Insert("SOME_DATA_KEY", data, null,
System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(2));
The first line shows how to use absolute object expiration time. The second line presents a sliding expiration time.
TIP Notice that the System.Web.Caching.Cache.NoSlidingExpiration constant is of type System.TimeSpan , and System.Web.Caching.Cache.NoAbsoluteExpiration is of type System.DateTime . As was mentioned earlier, Cache can manage memory and automatically remove objects from memory if an overflow occurs. An algorithm for this mechanism is encapsulated in the Cache object, and you cannot review or change it, but you're allowed to exert some influence on the process. You can do this with the help of the priority parameter in the Cache.Insert method. This one is of type System.Web.Caching.CacheItemPriority . An object of lower priority will be deleted from the cache faster than objects with higher priority. You can also prohibit deletion of an object from the cache. To set an object's priority in the cache, you use the enumeration System.Web.Caching.CacheItemPriority , which has the following values: Low , BelowNormal , Default , Normal , AboveNormal , High , and NotRemovable . An object of CacheItemPriority.NotRemovable priority will not be deleted during cache clearing. The last parameter of the Cache.Insert method to be discussed is the onRemoveCallback parameter of type System.Web.Caching.CacheItemRemovedCallback . This parameter takes as its value a delegate that will be invoked during the deletion of a particular object from the cache. This allows your code to notify you about the deletion of an object from the cache.
Cache.Insert("SOME_DATA_KEY", data, null, DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Low,
new System.Web.Caching.CacheItemRemovedCallback(SomeDelegate));
...
...
...
private void SomeDelegate(string key, object val,
System.Web.Caching.CacheItemRemovedReason reason)
{
...
}
Cache.Remove("SOME_DATA_KEY");
TIP You could remove an object from the cache manually by using the Cache.Remove method, which requires just one parameter: key (the name of object). The final capability of the Cache object discussed in this chapter is that Cache implements the IEnumerable interface. That is why you can very simply and quickly gain access to all objects located in the Cache object.
foreach(DictionaryEntry cacheEntry in Cache)
{
//access to key - cacheEntry.Key.ToString();
//access to value - Cache[cacheEntry.Key.ToString()].ToString();
}
|
|
|
|