AggregateCacheDependency 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
组合存储在 ASP.NET 应用程序的 Cache 对象中的项与一组 CacheDependency 对象之间的多个依赖关系。 此类不能被继承。
public ref class AggregateCacheDependency sealed : System::Web::Caching::CacheDependency
public sealed class AggregateCacheDependency : System.Web.Caching.CacheDependency
type AggregateCacheDependency = class
inherit CacheDependency
Public NotInheritable Class AggregateCacheDependency
Inherits CacheDependency
- 继承
示例
下面的代码示例使用 AggregateCacheDependency 类向依赖于文本文件和 XML 文件的缓存添加 DataSet命名 XMLDataSet
名称。
' When the page is loaded, use the
' AggregateCacheDependency class to make
' a cached item dependent on two files.
Sub Page_Load(sender As Object, e As EventArgs)
Dim Source As DataView
Source = Cache("XMLDataSet")
If Source Is Nothing
Dim DS As New DataSet
Dim FS As FileStream
Dim Reader As StreamReader
Dim txtDep As CacheDependency
Dim xmlDep As CacheDependency
Dim aggDep As AggregateCacheDependency
FS = New FileStream(Server.MapPath("authors.xml"),FileMode.Open,FileAccess.Read)
Reader = New StreamReader(FS)
DS.ReadXml(Reader)
FS.Close()
Source = new DataView(ds.Tables(0))
' Create two CacheDependency objects, one to a
' text file and the other to an XML file.
' Create a CacheDependency array with these
' two objects as items in the array.
txtDep = New CacheDependency(Server.MapPath("Storage.txt"))
xmlDep = New CacheDependency(Server.MapPath("authors.xml"))
Dim DepArray() As CacheDependency = {txtDep, xmlDep}
' Create an AggregateCacheDependency object and
' use the Add method to add the array to it.
aggDep = New AggregateCacheDependency()
aggDep.Add(DepArray)
' Call the GetUniqueId method to generate
' an ID for each dependency in the array.
msg1.Text = aggDep.GetUniqueId()
' Add the new data set to the cache with
' dependencies on both files in the array.
Cache.Insert("XMLDataSet", Source, aggDep)
If aggDep.HasChanged = True Then
chngMsg.Text = "The dependency changed at: " & DateTime.Now
Else
chngMsg.Text = "The dependency changed last at: " & aggDep.UtcLastModified.ToString()
End If
cacheMsg1.Text = "Dataset created explicitly"
Else
cacheMsg1.Text = "Dataset retrieved from cache"
End If
MyLiteral.Text = Source.Table.TableName
MyDataGrid.DataSource = Source
MyDataGrid.DataBind()
End Sub
Public Sub btn_Click(sender As Object, e As EventArgs )
If (MyTextBox.Text = String.Empty) Then
msg2.Text ="You have not changed the text file."
Else
msg2.Text="You added " & MyTextBox.Text & "."
' Create an instance of the StreamWriter class
' to write text to a file.
Dim sw As StreamWriter
sw = File.CreateText(Server.MapPath("Storage.txt"))
' Add some text to the file.
sw.Write("You entered:")
sw.WriteLine(MyTextBox.Text)
' Write arbitrary objects to the file as needed.
sw.Write("Text added at:")
sw.WriteLine(DateTime.Now)
sw.WriteLine("-------------------")
sw.Close()
End If
End Sub
注解
该 AggregateCacheDependency 类监视依赖项对象的集合,以便在其中任何一个对象发生更改时,缓存项会自动删除。 数组中的对象可以是 CacheDependency 对象、 SqlCacheDependency 对象、派生自 CacheDependency的自定义对象或这些对象的任何组合。
该 AggregateCacheDependency 类与 CacheDependency 类不同,因为它允许将不同类型的多个依赖项与单个缓存项相关联。 例如,如果创建一个从SQL Server数据库表和 XML 文件导入数据的页面,则可以创建一个SqlCacheDependency对象来表示数据库表的依赖项,以及CacheDependency表示对 XML 文件的依赖。 可以为每个依赖项创建一个 Cache.Insert 类实例 AggregateCacheDependency ,而不是为每个依赖项进行方法调用。 然后,可以使用单个 Insert 调用使页面依赖于 AggregateCacheDependency 实例。
构造函数
AggregateCacheDependency() |
初始化 AggregateCacheDependency 类的新实例。 |
属性
HasChanged |
获取一个值,该值指示 CacheDependency 对象是否已更改。 (继承自 CacheDependency) |
UtcLastModified |
获取依赖项的上次更改时间。 (继承自 CacheDependency) |