AggregateCacheDependency 类

定义

组合存储在 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

示例

下面的代码示例使用 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)

方法

Add(CacheDependency[])

CacheDependency 对象添加 AggregateCacheDependency 对象的数组。

DependencyDispose()

释放由 CacheDependency 类和从 CacheDependency 派生的所有类使用的资源。

(继承自 CacheDependency)
Dispose()

释放由 CacheDependency 对象使用的资源。

(继承自 CacheDependency)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
FinishInit()

完成 CacheDependency 对象的初始化。

(继承自 CacheDependency)
GetFileDependencies()

获取文件依赖项。

GetFileDependencies()

获取文件依赖项。

(继承自 CacheDependency)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
GetUniqueID()

检索 AggregateCacheDependency 对象的唯一标识符。

ItemRemoved()

删除监视的缓存项时调用。

(继承自 CacheDependency)
KeepDependenciesAlive()

更新依赖于此项的每个缓存项的上次访问时间。

(继承自 CacheDependency)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
NotifyDependencyChanged(Object, EventArgs)

通知 CacheDependency 基对象由派生的 CacheDependency 类表示的依赖项已更改。

(继承自 CacheDependency)
SetCacheDependencyChanged(Action<Object,EventArgs>)

添加 Action 方法,处理通知感兴趣的参与方有关此依赖项的更改。

(继承自 CacheDependency)
SetUtcLastModified(DateTime)

标记依赖项的上次更改时间。

(继承自 CacheDependency)
TakeOwnership()

允许第一个用户声明对此依赖项的独占拥有权。

(继承自 CacheDependency)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于