Recipe14.11.Caching Data with Multiple Dependencies


Recipe 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"); CacheDependency fileDep = new CacheDependency(file); 

Next a SqlCacheDependency is created for the pubs database and the Authors table using the method from Recipe 14.10:

 // Use our method from 14.10 to make a SqlCacheDependency. SqlCacheDependency sqlDep = CreateSqlCacheDependency(connStr); 

Then a DataSet reference is created and the code looks in the cache for it. If there is no DataSet already cached for this, a new DataSet is created and filled from the database, then populated from the XML file:

 // Set up data table to get. DataSet authorInfo = null; // Look for the pubs key in the cache. // If it isn't there, create it with a dependency // on a SQL Server table using the SqlCacheDependency class. // The "this" pointer refers to a Page class for a web page and // it accesses the System.Web.UI.Page.Cache property. if (this.Cache["authorInfo"] == null) {     // The data wasn't there so go get it and put it in the cache.     authorInfo = new DataSet("AuthorInfo");     using (SqlConnection sqlConn = new SqlConnection(connStr))     {         using (SqlDataAdapter adapter =             new SqlDataAdapter("SELECT * FROM AUTHORS", sqlConn))         {             adapter.Fill(authorInfo);             // Now add the royalty info.             authorInfo.ReadXml(file, XmlReadMode.InferSchema); 

Finally, an AggregateCacheDependency is created for the DataSet from the CacheDependency (fileDep) and the SqlCacheDependency (sqlDep). The DataSet is added to the cache with the AggregateCacheDependency (aggDep) and the cache takes care of managing the DataSet from this point forward. If a DataSet is in the cache already and the dependencies have not been triggered, the DataSet from the cache is returned.

             // Make the aggregate dependency so that if either the             // db or file changes, we toss this out of the cache.             AggregateCacheDependency aggDep = new AggregateCacheDependency();             // Add the two dependencies to the aggregate.             aggDep.Add(new CacheDependency[] { sqlDep, fileDep });             // Add author info dataset to cache with the aggregate             // dependency so that if either changes the cache will refetch.             this.Cache.Insert("authorInfo", authorInfo, aggDep);         }     } } else {     authorInfo = (DataSet)this.Cache["authorInfo"]; } 

Discussion

The AggregateCacheDependency class is new in the 2.0 Framework, and it is a welcome addition to an already strong caching arsenal for the ASP.NET cache. Almost the same effect could be accomplished in the 1.x Frameworks by adding a cache entry for each dependency, but then the data is being stored twice as well. AggregateCacheDependency allows for the user to specify more clearly what the dependencies are for a given item being cached. By not having to have a one-to-one ratio of dependencies to cache items anymore, the cache can be even leaner and perform better.

See Also

See Recipe 14.10; see the "AggregateCacheDependency Class," "CacheDependency Class," "SqlCacheDependency Class," and "DataSet" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

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