
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Caching Data with Multiple Dependencies
|
809
14.11 Caching Data with Multiple Dependencies
Problem
A dataset you are using in an ASP.NET page is comprised of data from multiple data
sources. You want to use caching for performance, but only one dependency object
can be added with the cache item.
Solution
Use the AggregateCacheDependency class to tie multiple cache dependencies together.
AggregateCacheDependency has an Add method that takes an array of CacheDependency
objects. This lets you write code that takes ordinary CacheDependency, SqlCacheDependency,
and even custom
CacheDependency objects, and the cached item is removed when any
of these dependencies changes.
For example, suppose that data for an author tracking system was being pulled from
an XML file with royalty information and from a database for the author addresses
and contact information. This data is combined into a single
DataSet that is used by
the ASP.NET code to produce a web page of author information. To make sure that
the page gets a
DataSet quickly, it is put in the cache once it is created.
First a
CacheDependency is created for the XML file with the royalty information:
// Make a dependency on the author royalties file
// so if someone updates it, the cached data will
// be disposed of.
string file = this.Server.MapPath("author_royalties.xml"); ...