AggregateCacheDependency、CacheDependency、SqlCacheDependency Asp.net 2.0和PetShop4 的缓存示例

我下面主要是对Asp.net 2.0 新增的缓存管理方面的内容和PetShop 4 的缓存处理的一个认识,大家一起来学习,交流才能进步,欢迎拍砖头。

   Asp.net 2.0新增的缓存管理:
   CacheDependency跟踪缓存依赖项,缓存依赖项可以是应用程序的 Cache 中的文件、目录或与其他对象的键。

   SqlCacheDependency类在所有受支持的 SQL Server 版本 (7.0, 2000, 2005) 上监视特定的 SQL Server 数据库表,以便在该表发生更改时,自动从 Cache 中删除与该表关联的项。 数据库表发生更改时,将自动删除缓存项,并向 Cache 中添加新版本的项。在使用 SQL Server 2005 数据库时,SqlCacheDependency 类还支持与 System.Data.SqlClient.SqlDependency 类进行集成。使用 SQL Server 2005 的查询通知机制来检测使 SQL 查询结果无效的数据更改。与 SQL 查询关联的任何缓存项都将从 System.Web.Caching.Cache 中移除。在使用 SQL Server 2005 时,可以使用 SqlCacheDependency 类向应用程序的 Cache 添加依赖于 SQL Server 数据库表或 SQL 查询的项

   AggregateCacheDependency类监视依赖项对象的集合,以便在任何依赖项对象更改时,该缓存项都会自动移除。数组中的对象可以是 CacheDependency对象、SqlCacheDependency 对象、从 CacheDependency派生的自定义对象或这些对象的任意组合.

AggregateCacheDependency类与 CacheDependency类的不同之处在于前者允许您将不同类型的多个依赖项与单个缓存项关联。例如,如果您创建一个从 SQL Server 数据库表和 XML 文件导入数据的页,则可创建一个 SqlCacheDependency对象来表示数据库表的依赖项,以及一个 CacheDependency来表XML 文件的依赖项。可创建 AggregateCacheDependency类的一个实例,将每个依赖项添加到该类中,而不是为每个依赖项调用 Cache.Insert 方法。然后,可使用单个Insert 调用使该页依赖于 AggregateCacheDependency实例。

AggregateCacheDependency示例:

 1  '  When the page is loaded, use the 
 2  '  AggregateCacheDependency class to make 
 3  '  a cached item dependent on two files.
 4 
 5  Sub  Page_Load(sender  As   Object , e  As  EventArgs)
 6      Dim  Source  As  DataView
 7 
 8     Source  =  Cache( " XMLDataSet " )
 9 
10      If  Source  Is   Nothing
11             Dim  DS  As   New  DataSet
12             Dim  FS  As  FileStream
13             Dim  Reader  As  StreamReader
14             Dim  txtDep  As  CacheDependency
15             Dim  xmlDep  As  CacheDependency
16             Dim  aggDep  As  AggregateCacheDependency
17 
18 
19            FS  =   New  FileStream(Server.MapPath( " authors.xml " ),FileMode.Open,FileAccess.Read)
20            Reader  =   New  StreamReader(FS)
21            DS.ReadXml(Reader)
22            FS.Close()
23 
24            Source  =   new  DataView(ds.Tables( 0 ))
25            '  Create two CacheDependency objects, one to a
26            '  text file and the other to an XML file. 
27            '  Create a CacheDependency array with these 
28            '  two objects as items in the array.
29            txtDep  =   New  CacheDependency(Server.MapPath( " Storage.txt " ))
30            xmlDep  =   New  CacheDependency(Server.MapPath( " authors.xml " ))
31             Dim  DepArray()  As  CacheDependency  =  {txtDep, xmlDep}
32 
33             '  Create an AggregateCacheDependency object and 
34             '  use the Add method to add the array to it.   
35            aggDep  =   New  AggregateCacheDependency()
36            aggDep.Add(DepArray)
37 
38             '  Call the GetUniqueId method to generate
39             '  an ID for each dependency in the array.
40            msg1.Text  =  aggDep.GetUniqueId()
41            
42             '  Add the new data set to the cache with 
43             '  dependencies on both files in the array.
44            Cache.Insert( " XMLDataSet " , Source, aggDep)
45             If  aggDep.HasChanged  =   True   Then
46               chngMsg.Text  =   " The dependency changed at:  "   &  DateTime.Now
47 
48             Else
49               chngMsg.Text  =   " The dependency changed last at:  "   &  aggDep.UtcLastModified.ToString()
50             End   If
51 
52 
53            cacheMsg1.Text  =   " Dataset created explicitly "
54           Else
55            cacheMsg1.Text  =   " Dataset retrieved from cache "
56           End   If
57 
58 
59            MyLiteral.Text  =  Source.Table.TableName
60            MyDataGrid.DataSource  =  Source
61            MyDataGrid.DataBind()
62         End Sub
63    
64   
65         Public   Sub  btn_Click(sender  As   Object , e  As  EventArgs )
66 
67          If  (MyTextBox.Text  =   String .Empty)  Then
68            msg2.Text  = " You have not changed the text file. "
69          Else
70            msg2.Text = " You added  "   &  MyTextBox.Text  &   " . "
71 
72             '  Create an instance of the StreamWriter class
73             '  to write text to a file.
74             Dim  sw  As  StreamWriter
75            sw  =  File.CreateText(Server.MapPath( " Storage.txt " ))
76 
77             '  Add some text to the file.
78            sw.Write( " You entered: " )
79            sw.WriteLine(MyTextBox.Text)
80 
81             '  Write arbitrary objects to the file as needed.
82            sw.Write( " Text added at: " )
83            sw.WriteLine(DateTime.Now)
84            sw.WriteLine( " ------------------- " )
85            sw.Close()
86          End   If
87  End Sub
88 

ASP.NET 2.0 允许您使用 SqlCacheDependency 类创建依赖于数据库中表或行的缓存项。当表中或特定行中发生更改时,带有依赖项的项便会失效,并会从缓存中移除。可以在 Microsoft SQL Server 7.0SQL Server 2000 SQL Server 2005 中设置表的依赖项。如果您使用 SQL Server 2005,还可以设置特定记录的依赖项。

ASP.NET 2.0 SQL 缓存依赖项提供以下功能:

1.      SQL 缓存依赖项可用于应用程序缓存和页输出缓存。

2.      可在 SQL Server 7.0 及更高版本中使用 SQL 缓存依赖项。

3.      可以在网络园(一台服务器上存在多个处理器)或网络场(多台服务器运行同一应用程序)中使用 SQL 缓存依赖项。

4.      SQL 缓存依赖项关联的数据库操作比较简单,因此不会给服务器带来很高的处理成本。

ASP.NET 2.0 SQL Server 7.0 SQL Server 2000 的缓存依赖项实现了一个轮询模型。ASP.NET 进程内的一个线程会以指定的时间间隔轮询 SQL Server 数据库,以确定数据是否已更改。如果数据已更改,缓存依赖项便会失效,并从缓存中移除。可以在 Web.config 文件中以声明方式指定应用程序中的轮询间隔,也可以使用 SqlCacheDependency类以编程方式指定此间隔。

对于 SQL Server 7.0 SQL Server 2000SQL 缓存依赖项仅限于表级别的数据更改。可以将 ASP.NET 配置为轮询数据库来确定表中的更改,但不能确定特定行中的更改。

启用 SQL 缓存

为了在 SQL Server 7.0 SQL Server 2000 中使用 SQL 缓存依赖项,必须先将 SQL Server 配置为支持缓存依赖项。ASP.NET 提供了一些实用工具,可用于配置 SQL Server 上的 SQL 缓存,其中包括一个名为 Aspnet_regsql.exe 的工具和 SqlCacheDependencyAdmin.

SQL Server 2005 为缓存依赖项实现的模型不同于 SQL Server 7.0 SQL Server 2000 中的缓存依赖项模型。在 SQL Server 2005 中,不需要执行任何特殊的配置步骤来启用 SQL 缓存依赖项。此外,SQL Server 2005 还实现了一种更改通知模型,可以向订阅了通知的应用程序服务器发送通知,而不是依赖早期版本的 SQL Server 中必需的轮询模型。

SQL Server 2005 缓存依赖项在接收通知的更改类型方面更具灵活性。SQL Server 2005 监控对特定 SQL 命令的结果集的更改。如果数据库中发生了将修改该命令的结果集的更改,依赖项便会使缓存的项失效。此功能使得 SQL Server 2005 可以提供行级别的通知。

MS PetShop4.0 缓存的管理

PetShop 中,需要对数据表实现Cache。这些Cache都存放在.NetWeb系统提供的Cache集合对象 System.Web.Caching.AggregateCacheDependency中,下面代码中的红色标明的代码就是 AggregateCacheDependency缓存管理的代码。

 1  ///   <summary>
 2       ///  This is the base class for SQL2KCacheDependency implementation that encapsulates common
 3       ///  algorithm to retrieve database and table names from configuration file and create
 4       ///  the necessary AggregateCacheDependency object
 5       ///   </summary>
 6       public   abstract   class  TableDependency : PetShop.ICacheDependency.IPetShopCacheDependency {
 7           //  This is the separator that's used in web.config
 8           protected   char [] configurationSeparator  =   new   char [] {  ' , '  };
 9   
10          protected AggregateCacheDependency dependency = new AggregateCacheDependency();
11   
12           ///   <summary>
13           ///  The constructor retrieves all related configuration and add CacheDependency object accordingly
14           ///   </summary>
15           ///   <param name="configKey"> Configuration key for specific derived class implementation </param>
16           protected  TableDependency( string  configKey) {
17   
18               string  dbName  =  ConfigurationManager.AppSettings[ " CacheDatabaseName " ];
19               string  tableConfig  =  ConfigurationManager.AppSettings[configKey];
20               string [] tables  =  tableConfig.Split(configurationSeparator);
21   
22               foreach  ( string  tableName  in  tables)
23                  dependency.Add( new  SqlCacheDependency(dbName, tableName));
24          }
25   
26           public  AggregateCacheDependency GetDependency() {
27               return  dependency;
28          }
29  }
30 
PetShop4 的配置文件:
 1  < appSettings >
 2    
 3     <!--  Enable data caching  -->
 4     < add  key ="EnableCaching"  value ="true" />
 5     <!--  Cache duration (in hours-whole number only)  -->
 6     < add  key ="CategoryCacheDuration"  value ="12" />
 7     < add  key ="ProductCacheDuration"  value ="12" />
 8     < add  key ="ItemCacheDuration"  value ="12" />
 9     <!--  Cache dependency options. Possible values: PetShop.TableCacheDependency for SQL Server and keep empty for ORACLE  -->
10     < add  key ="CacheDependencyAssembly"  value ="PetShop.TableCacheDependency" />
11     <!--  CacheDatabaseName should match the name under caching section, when using TableCacheDependency  -->
12     < add  key ="CacheDatabaseName"  value ="MSPetShop4" />
13     <!--  *TableDependency lists table dependency for each instance separated by comma  -->
14     < add  key ="CategoryTableDependency"  value ="Category" />
15     < add  key ="ProductTableDependency"  value ="Product,Category" />
16     < add  key ="ItemTableDependency"  value ="Product,Category,Item" />
17     </ appSettings >
18 

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值