请单击以进行评分并提供反馈

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2008/.NET Framework 3.5

同时提供下列产品的其他版本:
.NET Framework 类库
AggregateCacheDependency 类

更新:2007 年 11 月

组合 ASP.NET 应用程序的 Cache 对象中存储的项和 CacheDependency 对象的数组之间的多个依赖项。无法继承此类。

命名空间:  System.Web.Caching
程序集:  System.Web(在 System.Web.dll 中)

Visual Basic(声明)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class AggregateCacheDependency _
    Inherits CacheDependency
Visual Basic (用法)
Dim instance As AggregateCacheDependency
C#
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class AggregateCacheDependency : CacheDependency
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class AggregateCacheDependency sealed : public CacheDependency
J#
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public final class AggregateCacheDependency extends CacheDependency
JScript
public final class AggregateCacheDependency extends CacheDependency

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

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

下面的代码示例使用 AggregateCacheDependency 类将名为 XMLDataSetDataSet 添加到依赖于文本文件和 XML 文件的缓存中。

Visual Basic
' 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

System..::.Object
  System.Web.Caching..::.CacheDependency
    System.Web.Caching..::.AggregateCacheDependency
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

.NET Framework

受以下版本支持:3.5、3.0、2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker