Title: Implement Data Driven Caching with Visual Studio 2003 and 2005
1Implement Data Driven Caching with Visual Studio
2003 and 2005
- Stephen Forte
- CTO Corzen Inc
- Microsoft Regional Director NY Region (USA)
2Agenda
- ASP.NET Data Controls
- About the ASP .NET Cache Engine
- Output Caching
- SQLCacheDependency
3Binding in a Stateless Environment
- Binding in ASP.NET is really a metaphor
- Controls are not so much actively bound as they
are associated with a data source - Binding manager is really a server-side HTML
generation engine - Must use caching or session state to persist data
changes - Code required to implement bound control events
like Edit, Delete, Update - Adding rows takes special effort
4ASP.NET DataGrid
- Lightweight list-bound server control that will
display data in a table of columns and rows - Provides style and appearance properties
- Provides sorting and paging
- Provides selection and editing
- Fully programmable
- Datagrid drastically reduces the code you have to
write vs. ASP Classic
5Populating the DataGrid
6Simplified Data Binding
- Data binding expressions are now simpler and
support hierarchical (XML) data binding
lt!-- ASP.NET 1.x data binding expression --gt lt
DataBinder.Eval (Container.DataItem, "Price")
gt lt!-- Equivalent ASP.NET 2.0 data binding
expression --gt lt Eval ("Price") gt lt!-- XML
data binding --gt lt XPath ("Price") gt
7DataSource Controls
- Declarative (no-code) data binding
Name
Description
SqlDataSource
Connects data-binding controls to SQL databases
AccessDataSource
Connects data-binding controls to Access databases
XmlDataSource
Connects data-binding controls to XML data
ObjectDataSource
Connects data-binding controls to data components
SiteMapDataSource
Connects site navigation controls to site map data
8SqlDataSource
- Declarative data binding to SQL databases
- Any database served by a managed provider
- Two-way data binding
- SelectCommand defines query semantics
- InsertCommand, UpdateCommand, and DeleteCommand
define update semantics - Optional caching of query results
- Parameterized operation
9Using SqlDataSource
ltaspSqlDataSource ID"Titles" RunAt"server"
ConnectionString"serverlocalhostdatabasepubsi
ntegrated securitytrue" SelectCommand"select
title_id, title, price from titles"
/gt ltaspDataGrid DataSourceID"Titles"
RunAt"server" /gt
10Populating the GridView
11Agenda
- ASP.NET Data Controls
- About the ASP .NET Cache Engine
- Output Caching
- SQLCacheDependency
12What is the ASP .NET Cache Engine?
- Ability to save versions of pages in cache and
not have to work to rerender the page - The ASP.NET cache is private to each application
and stores objects in memory - The lifetime of the cache is equivalent to the
lifetime of the application - When the application is restarted, the cache is
recreated - Single most inexpensive way to dramatically
increase performance of your ASP .NET Application
13Designing for Caching
- Leverage the built-in ASP.NET built-in caching
features - Output Caching
- Fragment Caching
- Cache API
- Recommendation
- Specifically design your pages around these
features can lead to massive perf wins
14Agenda
- ASP.NET Data Controls
- About the ASP .NET Cache Engine
- Output Caching
- SQLCacheDependency
15Output Caching
- Caches the static result of an ASP.NET page
- Declarative lt_at_ OutputCache gt directive
- Optional Output Cache APIs can also be called
programmatically - Duration- The amount of time in seconds in which
a page may be output cached. - This time period is calculated by taking the time
of the request, if the page is not already in the
cache, and adding the number of seconds. - The duration value also sets the HTTP expires
header.
16Output Caching Options
- Caching Options
- Duration
- Time item exists in the cache
- Remember Scavenging
- VaryByParam
- Varies cache entries by Get/Post params
- Name param, separate by semi-colons, supports
- VaryByHeader
- Varies cache entries by Http header
- VaryByCustom
- Override method within Global.asax to custom vary
by whatever you want (you control the cache key)
17Caching Options
- VaryByParam
- VaryByHeader
- VaryByCustom
- File Dependency
- Fragmented Caching
18Agenda
- ASP.NET Data Controls
- About the ASP .NET Cache Engine
- Output Caching
- SQLCacheDependency
19SqlDataSource and Caching
- SqlDataSource supports declarative caching of
results through these properties
Name
Description
EnableCaching
Specifies whether caching is enabled (default
false)
CacheDuration
Length of time in seconds results should be cached
CacheExpirationPolicy
Specifies whether cache duration is sliding or
absolute
CacheKeyDependency
Creates dependency on specified cache key
SqlCacheDependency
Creates dependency on specified database entity
20Caching Query Results
ltaspSqlDataSource ID"Countries" RunAt"server"
ConnectionString"serverlocalhostdatabasenorth
wind..." SelectCommand"select distinct
country from customers order by country"
EnableCaching"true" CacheDuration"60"
/gt ltaspDropDownList ID"MyDropDownList"
DataSourceID"Countries" DataTextField"country"
AutoPostBack"true" RunAt"server" /gt
21SQL Cache Dependencies
- New cache dependency type
- Embodied in SqlCacheDependency class
- Configured through ltsqlCacheDependencygt
configuration section - Links cached items to database entities
- ASP.NET application cache
- ASP.NET output cache
- Compatible with SQL Server 7, 2000, 2005
22Preparing a Database
- Use Aspnet_regsql.exe or SqlCache-DependencyAdmin
to prepare database
aspnet_regsql -S localhost -E -d Northwind -ed
Server name
Trusted connection
Database name
Enable database
Not necessary for SQL Server 2005
23Preparing a Table
- Use Aspnet_regsql.exe or SqlCache-DependencyAdmin
to prepare table
aspnet_regsql -S localhost -E -d Northwind -t
Products -et
Server name
Trusted connection
Database name
Table name
Enable table
Not necessary for SQL Server 2005
24Preparing Web.config
ltconfigurationgt ltconnectionStringsgt ltadd
name"Northwind" connectionString"serverlo
calhostdatabasenorthwind..." /gt
lt/connectionStringsgt ltsystem.webgt
ltcachinggt ltsqlCacheDependency
enabled"true" pollTime"5000"gt
ltdatabasesgt ltadd name"Northwind"
connectionStringName"Northwind" /gt
lt/databasesgt lt/sqlCacheDependencygt
lt/cachinggt ltsystem.webgt lt/configurationgt
25Using SqlCacheDependency with the Application
Cache
Cache.Insert ("Products", products, new
SqlCacheDependency ("Northwind", "Products")
Database name
Table name
26Using SqlCacheDependency with the Output Cache
lt_at_ OutputCache Duration"60" VaryByParam"None"
SqlDependency"NorthwindProducts" gt
Database name
Table name
27Using SqlCacheDependency with SqlDataSource
ltaspSqlDataSource ID"Countries" RunAt"server"
ConnectionString"serverlocalhostdatabasenorth
wind..." SelectCommand"select distinct
country from customers order by country"
EnableCaching"true" CacheDuration"60000"
SqlCacheDependency"NorthwindCustomers"
/gt ltaspDropDownList ID"MyDropDownList"
DataSourceID"Countries" DataTextField"country"
AutoPostBack"true" RunAt"server" /gt
Database name
Table name
28SQLCacheDependency
29Cache Configuration
- ltcachegt
- Enable/disable application cache
- Enable/disable item expiration and more
- ltoutputCachegt, ltoutputCacheSettingsgt
- Enable/disable output caching
- Enable/disable disk-based persistence
- Set maximum size per app and more
- ltsqlCacheDependencygt
30Session Summary
- Data Controls provide a much simpler way to code
- Output cache is real simple and powerful
- A lot of flexibility without any code
- Fragmented cache for partial page cache
- The Future holds integration with SQL Server
31Questions?
32Thanks!
- Please fill out your evaluation form!
- stevef_at_orcsweb.com
- Please put (PDC Karachi in the subject)