Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Learn Better Ways to Manage Your Cache in ASP.NET : Page 2

Learn how to use caching effectively today and tomorrow with some new (and some not-quite-as-new) cache management techniques for ASP.NET 1.x and 2.0.


advertisement
Technique #2: Cache Data Using the Cache Object
(ASP.NET 1.x and ASP.NET 2.0)
Output caching has its applications but sometimes it's more useful to explicitly cache the data that you would use frequently using the Cache object.

For example, suppose I have a text file named announcement.txt that contains messages to be displayed to the user when the main page of my Web site is loaded. Instead of loading this page every time a user visits my site (performing an I/O operation to read the announcement file is a computationally expensive task), I would cache the content of the file the first time it is read. However, the content of this file would change over time, and hence I need to have a method of invalidating the cache if the content of the file changes.

ASP.NET supports the CacheDependency class that allows the cache object to be linked to a dependency. In my case, the dependency is on the file announcement.txt.



Here is the code to read the announcement from the file, cache it, and add a dependency to it:

Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles Me.Load displayAnnouncement() End Sub Public Sub displayAnnouncement() Dim announcement As String If Cache("announcement") Is Nothing Then Dim file As New _ System.IO.StreamReader _ (Server.MapPath("announcement.txt")) announcement = file.ReadToEnd file.Close() Dim depends As New _ System.Web.Caching.CacheDependency _ (Server.MapPath("announcement.txt")) Cache.Insert("announcement", announcement, depends) End If Response.Write(CType(Cache("announcement"), String)) End Sub

When the page loads for the first time, the content of the file is cached. On a subsequent request, the content is taken from the cache. But if the content of the file has changed, the cache is invalidated and the file is read and cached again.

Expiration Dates: Invalidating the Cache
The previous example shows that the cache will only be invalidated when the content of the file changes. Another method to invalidate the cache is to use either Absolute Expiration or Sliding Expiration.

Absolute Expiration allows you to specify the duration of the cache, starting from the time the cache is activated. The following example shows that the cache has a cache dependency specified, as well as an expiration time of one minute (obviously, I'm using a one-minute cache merely for example purposes—you'll want to set yours appropriately).

'===Absolute expiration=== Cache.Insert("announcement", announcement, depends, _ DateTime.Now.AddMinutes(1), Nothing)

Sliding Expiration specifies that the cache will expire if a request is not made within a specified duration. Sliding expiration policy is useful whenever you have a large number of items that need to be cached, because this policy enables you to keep only the most frequently accessed items in memory. For example, the following code specifies that the cache will have a sliding duration of one minute. If a request is made 59 seconds after the cache is accessed, the validity of the cache would be reset to another minute:

'===Sliding expiration=== Cache.Insert("announcement", announcement, depends, _ DateTime.MaxValue, _ TimeSpan.FromMinutes(1))

Technique #3: Caching Page Fragments
(ASP.NET 2.0 only)
Figure 2. Two Timing: This page's cache captures only some of the data.
In ASP.NET 2.0, you now have the capability to cache parts of a page, rather than the entire page as discussed earlier. For example, the following code sample specifies caching the page output every 15 seconds. I've also included a shared function called RealTime (which takes in a System.Web.HttpContext parameter) that returns the time.

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <%@ OutputCache Duration="15" VaryByParam = "none" %> <script runat="server"> Public Shared Function RealTime(ByVal context As _ System.Web.HttpContext) As String Return "Real Time is :" & Now.ToString End Function Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Response.Write("Time (cached) is :" & Now) Response.Write("<br/>") Response.WriteSubstitution(New _ HttpResponseSubstitutionCallback _ (AddressOf RealTime)) End Sub </script>

If you were to load this page and refresh it a few times, you'd notice that the time written on the top line only updates every 15 seconds, regardless of how often you refesh, while the time on the second line updates every time you refresh (see Figure 2). The WriteSubstitution method (from the Response object) allows fragments of a page to be executed and the output inserted into the cache output page.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap