Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

SQL Cache Invalidation

Performance is key for any application or piece of code that you develop. The browser helps with client-side caching of text and images, whereas the server-side caching you choose to implement is vital for creating the best possible performance. Caching is the process of storing frequently used data on the server to fulfill subsequent requests. You will discover that grabbing objects from memory is much faster than re-creating the Web pages or items contained in them from scratch each and every time. Caching increases your application’s performance, scalability, and availability. The more you fine-tune your application’s caching approach, the better it performs.

ASP.NET 2.0 introduces SQL Server cache invalidation that enables you to create a cache based upon items contained within a Microsoft SQL Server database and also to invalidate it if any changes occur in the database. This is something frequently requested by developers using ASP.NET 1.0/1.1, and the ASP.NET team worked hard to bring it to ASP.NET 2.0. This chapter takes a close look at this unique aspect of caching.

Caching in ASP.NET 1.0/1.1

In ASP.NET 1.0/1.1, developers deal with caching in several ways. First, you can cache an entire HTTP response (the entire Web page) using a mechanism called output caching. Two other methods are partial page caching and data caching. These methods are described in the following sections.

Output caching

Output caching is a way to keep the dynamically generated page content in the server’s memory for later retrieval. After a cache is saved to memory, it can be used when any subsequent requests are made to the server. You apply output caching using the OutputCache directive as follows:

<%@ OutputCache Duration=”60” VaryByParam=”None” %>

Chapter 11

You apply output caching by inserting an OutputCache page directive at the top of an .aspx page. The Duration attribute defines the number of seconds that a page is stored in memory. The VaryByParam attribute determines which versions of the page output are actually cached. You can generate different responses based on whether an HTTP-POST or HTTP-GET response is required. Other than the attributes for the OutputCache directive, ASP.NET 1.0/1.1 includes the VaryByHeader, VaryByCustom, and Location attributes.

Partial page caching

Similar to output caching, partial page caching enables you to cache only specific blocks of a Web page. You can, for example, cache only the center of the page. Partial page caching is achieved with the caching of user controls. You can build your ASP.NET pages utilizing numerous user controls and then apply output caching to the user controls that you select. This, in essence, only caches the parts of the page that you want, while leaving other parts of the page outside the reach of caching. This is a nice feature, and if done correctly, it can lead to pages that perform better.

Data caching using the Cache object

The other method of caching is using the Cache object to start caching specific data items for later use on a particular page or group of pages. The Cache object enables you to store everything from simple name/value pairs to more complex objects like datasets and even entire .aspx pages.

The Cache object is used in the following fashion:

Cache(“WhatINeedToStore”) = myDataSet

After an item is in the cache, you can retrieve it later as shown here:

Dim ds As New DataSet

ds = CType(Cache(“WhatINeedToStore”), DataSet)

The Cache object is an outstanding way to cache your pages and is, in fact, what the OutputCache directive uses under the covers.

Cache dependencies

Using the Cache object, you can store items in the cache and invalidate these items in the cache based on several different dependencies. In ASP.NET 1.0/1.1, the only possible dependencies include

File-based dependencies

Key-based dependencies

Time-based dependencies

When inserting items into the cache using the Cache object, you set the dependencies with the Insert method, as shown in the following example:

Cache.Insert(“DSN”, connectionString, _

New CacheDependency(Server.MapPath(“myconfig.xml”)))

324

SQL Cache Invalidation

By using a dependency, when the item being referenced changes, the cache for that item is removed from memory.

ASP.NET 2.0 unseals the CacheDependency class

The big change in ASP.NET 2.0 in the area of caching is that the CacheDependency class has been unsealed (or made overrideable). You can now create classes that inherit from the CacheDependency class and create more elaborate dependencies that are not limited to the Time, Key, or File dependencies of the past.

When you create your own cache dependencies, you have the option to add procedures for such things as Web services data, only-at-midnight dependencies, or textual string changes within a file. The dependencies you create are limited only by your imagination. The unsealing of the CacheDependency class has provided all this for you.

Because of the unsealing of the CacheDependency class, the ASP.NET team has built a new SQL Server cache dependency — SqlCacheDependency. A SQL cache dependency was the most requested caching feature from ASP.NET 1.0/1.1 developers. You can now determine that a cache becomes invalid whenever a table changes within the underlying SQL Server.

Using the SQL Ser ver Cache Dependency

To utilize the new SQL Server cache dependency feature in ASP.NET 2.0, you must perform a one-time setup of your SQL Server database. To set up your SQL Server, use the aspnet_regsqlcache.exe tool found at C:\WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx. This tool makes the necessary modifications to SQL Server so that you can start working with the new SQL cache invalidation features.

To get at the aspnet_regsqlcache.exe tool, open up the Visual Studio Command Prompt by choosing Start All Programs Microsoft Visual Studio 2005 Visual Studio Tools Visual Studio Command Prompt. After you get the command prompt, type this command:

aspnet_regsqlcache.exe -?

This pulls up the help command list (see Figure 11-1).

Figure 11-1

325

Chapter 11

The following table details the commands available in the aspnet_regsqlcache.exe tool.

Command

Description

 

 

-S server

The server where the SQL cache invalidation setup is to occur.

-U login id

The SQL Server login ID.

-P password

The SQL Server password.

-E trusted connection

Instructions for using the current Windows credentials for

 

authentication.

-t table name

The name of the table you want to work with for SQL Server

 

cache invalidation.

-d database

The name of the database.

-ed

A command to enable a database for SQL cache dependency.

-dd

A command to disable a database for SQL cache dependency.

-et

A command to enable a table for SQL cache dependency.

-dt

A command to disable a table for SQL cache dependency.

-lt

A list of all the tables enabled for SQL cache dependency.

-?

A list of available option commands.

 

 

The following sections show you how to use some of these commands.

Enabling databases for SQL Server cache invalidation

To use SQL Server cache invalidation, begin with two steps. The first step enables the appropriate database. Next, you enable the tables that you want to work with. You must perform both steps for this process to work. Start by enabling the database.

If you want to enable your databases for SQL cache invalidation and you are working on the computer where the SQL Server instance is located, you use the following construct:

aspnet_regsqlcache.exe –S localhost –U sa –P password –d Northwind –ed

This produces something similar to what you see in Figure 11-2.

Figure 11-2

326