- •Contents
- •Introduction
- •Acknowledgments
- •The Goals of ASP.NET 2.0
- •Developer productivity
- •Administration and management
- •Performance and scalability
- •Device-specific code generation
- •Additional New Features of ASP.NET 2.0
- •New developer infrastructures
- •New compilation system
- •Additions to the page framework
- •New objects for accessing data
- •New server controls
- •A New IDE for Building ASP.NET 2.0 Pages
- •The Document Window
- •Views in the Document Window
- •The tag navigator
- •Page tabs
- •Code change status notifications
- •Error notifications and assistance
- •The Toolbox
- •The Solution Explorer
- •Lost Windows
- •Other Common Visual Studio Activities
- •Creating new projects
- •Making references to other objects
- •Using smart tags
- •Saving and importing Visual Studio settings
- •Application Location Options
- •Built-in Web server
- •Web site requiring FrontPage Extensions
- •The ASP.NET Page Structure Options
- •Inline coding
- •New code-behind model
- •New Page Directives
- •New attributes
- •New directives
- •New Page Events
- •Cross-Page Posting
- •New Application Folders
- •\Code folder
- •\Themes folder
- •\Resources folder
- •Compilation
- •The New Data Source Controls
- •The SqlDataSource and GridView Controls
- •Reading data
- •Applying paging in the GridView
- •Sorting rows in the GridView control
- •Defining bound columns in the GridView control
- •Enabling the editing of rows in the GridView control
- •Deleting data from the GridView
- •Dealing with other column types in the GridView
- •Selecting which fields to display in the DetailsView control
- •Using the GridView and DetailsView together
- •Updating, inserting, and deleting rows
- •XmlDataSource Control
- •ObjectDataSource Control
- •SiteMapDataSource Control
- •DataSetDataSource Control
- •Visual Studio 2005
- •Connection Strings
- •Site Maps
- •The PathSeparator property
- •The PathDirection property
- •The ParentLevelsDisplayed property
- •The ShowToolTips property
- •Examining the parts of the TreeView control
- •Binding the TreeView control to an XML file
- •Selecting multiple options in a TreeView
- •Specifying custom icons in the TreeView control
- •Specifying lines used to connect nodes
- •Working with the TreeView control programmatically
- •Applying different styles to the Menu control
- •Menu Events
- •Binding the Menu control to an XML file
- •SiteMap Data Provider
- •SiteMapViewType
- •StartingNodeType
- •SiteMap API
- •Why Do You Need Master Pages?
- •The Basics of Master Pages
- •Coding a Master Page
- •Coding a Content Page
- •Mixing page types and languages
- •Specifying which master page to use
- •Working with the page title
- •Working with controls and properties from the master page
- •Nesting Master Pages
- •Container-Specific Master Pages
- •Event Ordering
- •Caching with Master Pages
- •Using ASP.NET 2.0 Packaged Themes
- •Applying a theme to a single ASP.NET page
- •Applying a theme to an entire application
- •Applying a theme to all applications on a server
- •Removing themes from server controls
- •Removing themes from Web pages
- •Removing themes from applications
- •Creating Your Own Themes
- •Creating the proper folder structure
- •Creating a skin
- •Including CSS files in your themes
- •Having your themes include images
- •Defining Multiple Skin Options
- •Programmatically Working with Themes
- •Themes and Custom Controls
- •Authentication
- •Authorization
- •ASP.NET 2.0 Authentication
- •Setting up your Web site for membership
- •Adding users
- •Asking for credentials
- •Working with authenticated users
- •Showing the number of users online
- •Dealing with passwords
- •ASP.NET 2.0 Authorization
- •Using the LoginView server control
- •Setting up your Web site for role management
- •Adding and retrieving application roles
- •Deleting roles
- •Adding users to roles
- •Getting all the users of a particular role
- •Getting all the roles of a particular user
- •Removing users from roles
- •Checking users in roles
- •Using the Web Site Administration Tool
- •The Personalization Model
- •Adding a simple personalization property
- •Using personalization properties
- •Adding a group of personalization properties
- •Using grouped personalization properties
- •Defining types for personalization properties
- •Using custom types
- •Providing default values
- •Making personalization properties read-only
- •Anonymous Personalization
- •Enabling anonymous identification of the end user
- •Working with anonymous identification events
- •Anonymous options for personalization properties
- •Migrating Anonymous Users
- •Personalization Providers
- •Working with the Access personalization provider
- •Working with the SQL Server personalization provider
- •Using multiple providers
- •Building Dynamic and Modular Web Sites
- •Introducing the WebPartManager control
- •Working with zone layouts
- •Understanding the WebPartZone control
- •Explaining the WebPartPageMenu control
- •Modifying zones
- •Caching in ASP.NET 1.0/1.1
- •Output caching
- •Partial page caching
- •Data caching using the Cache object
- •Cache dependencies
- •ASP.NET 2.0 unseals the CacheDependency class
- •Enabling databases for SQL Server cache invalidation
- •Enabling tables for SQL Server cache invalidation
- •Looking at SQL Server
- •Looking at the tables that are enabled
- •Disabling a table for SQL Server cache invalidation
- •Disabling a database for SQL Server cache invalidation
- •Configuring your ASP.NET Application
- •Adding more than one table to a page
- •Attaching SQL Server cache dependencies to the Request object
- •Attaching SQL Server cache dependencies to the Cache object
- •Customizing the side navigation
- •Examining the AllowReturn attribute
- •Working with the StepType attribute
- •Adding a header to the Wizard control
- •Utilizing Wizard control events
- •Working with images from disk
- •Resizing images
- •Displaying images from streams
- •The MMC ASP.NET Snap-In
- •General
- •Custom Errors
- •Authorization
- •Authentication
- •Application
- •State Management
- •Advanced
- •ASP.NET Web Site Administration Tool
- •Home
- •Security
- •Profile
- •Application
- •Provider
- •Managing the Site Counter System
- •Generics
- •Iterators
- •Anonymous Methods
- •Operator Overloading
- •Visual Basic XML Documentation
- •New Visual Basic Keywords
- •Continue
- •Using
- •Global
- •Index
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
