What Is Caching?

only for RuBoard

Caching isn't a new term to developers, but, historically, caching dynamic Web pages hasn't been a very easy or manageable task. Caching is the method of saving data of any kind in memory. When the cached data is needed, instead of re-creating the data, the application provides it to the client or requestor from the in-memory version of the data. The in-memory data is referred to as the cached version. The data can range from a letter in the alphabet to a complete Web page.

Implementing caching in your application increases performance because you can serve Web pages to clients without having to re-create the pages on every request. For instance, if you have a page that is dynamically built through a database query, but the data rarely changes, why not cache the entire page on the first request of the day and serve every additional request for the page to the client from the cache? Then, at midnight, take the page out of the cache and do it again the next day.

Using this technique has many advantages. The first is that you limit the amount of database calls each day. If that page gets requested 20,000 times per day, you will limit the number of expensive calls to your database by that many calls. This alone should be enough to convince you that caching is a good thing, but there is more.

Pages that implement caching are served more quickly to the client because the pages and data used to create them are served from a memory resident version. The following list contains some frequently asked questions regarding caching. I thought I would answer these questions for you now, and explain their details later:


Do I have control over how long items are in the cache?


Yes, you can set varying expiration times for cache items. For instance, you can cache one item for 10 seconds and another for 10 hours.


What happens if I change the code in a cached page?


If you change the code for an item that is in the cache, .NET will recognize the change and remove the item from the cache. The first new request for the page will re-cache the page, and the user will never know that the code changed.


Can I cache based on certain criteria?


Yes, there are many techniques available to cache pages based on any number of criteria. For example, you can vary the cache based on query string values or by browser types or combinations.


Can I cache pages only?


No, data caching using the Cache APIs enables you to cache objects such as a DataSet , an Array , or user controls.


After I put an item into the cache, can I take it out?


Yes, just as you have complete control over how and when an item is cached, you also have the ability to take items out of the cache whenever you want.

I can talk about caching until my head explodes, but I bet you want to see some source code. Listing 16.1 contains a very basic example of how to implement caching within a Web Form. This example demonstrates the bare minimum that you have to do to enable output caching.

Listing 16.1 Using the @OutputCache Directive to Enable Page Output Caching
 [VisualBasic.NET & C#.NET] 01: <%@ OutputCache Duration="60" VaryByParam="none"%> 02: <html> 03: <body> 04: This page was cached at: 05: <b> 06:  <%=DateTime.Now.ToString("G")%> 07: </b> 08: </body> 09: </html> 

Caching in Listing 16.1 is enabled through the use of the @OutputCache directive on line 1. This line accomplishes three things necessary to enable output caching on your server using the @OutputCache directive. First, it supplies a valid expiration policy through the use of the Duration attribute, which is a required attribute. Second, the cache is public cache visible (see HttpCacheability enumeration ”covered later in the chapter). This is done automatically by using the @OutputCache directive. Third, a value was given for the VaryByParam attribute (another required attribute which varies how documents are cached based on parameters) ” none was supplied as a value so only one version of the page will be cached.

The rendered page will display the current server time; the time displayed will not change if the page is refreshed before the expiration policy has expired (60 seconds), or the Web application is restarted. You can test this by refreshing the page before 60 seconds have elapsed, and then again after 60 seconds have elapsed. Only after the 60 seconds have elapsed will the time printed to the page change to the current server time again.

I am sure that you have many more questions, but I am also sure that, after you are finished with this chapter, each of your questions will be answered exhaustively. Now let's begin by discussing page output caching.

only for RuBoard

Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net