AggregateCacheDependency Class

AggregateCacheDependency Class

Combines multiple dependencies between an item stored in an ASP.NET application's Cache object and an array of CacheDependency objects. This class cannot be inherited.


Namespace:  System.Web.Caching
Assembly:  System.Web (in System.Web.dll)

public sealed class AggregateCacheDependency : CacheDependency

The AggregateCacheDependency type exposes the following members.

Public methodAggregateCacheDependencyInitializes a new instance of the AggregateCacheDependency class.

Public propertyHasChangedGets a value indicating whether the CacheDependency object has changed. (Inherited from CacheDependency.)
Public propertyUtcLastModifiedGets the time when the dependency was last changed. (Inherited from CacheDependency.)

Public methodAddAdds an array of CacheDependency objects to the AggregateCacheDependency object.
Protected methodDependencyDisposeReleases the resources used by the CacheDependency class and any classes that derive from CacheDependency. (Inherited from CacheDependency.)
Public methodDisposeReleases the resources used by the CacheDependency object. (Inherited from CacheDependency.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected methodFinishInitCompletes initialization of the CacheDependency object. (Inherited from CacheDependency.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetUniqueIDRetrieves a unique identifier for the AggregateCacheDependency object. (Overrides CacheDependency.GetUniqueID().)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodNotifyDependencyChangedNotifies the base CacheDependency object that the dependency represented by a derived CacheDependency class has changed. (Inherited from CacheDependency.)
Protected methodSetUtcLastModifiedMarks the time when a dependency last changed. (Inherited from CacheDependency.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The AggregateCacheDependency class monitors a collection of dependency objects so that when any of them change, the cached item is automatically removed. The objects in the array can be CacheDependency objects, SqlCacheDependency objects, custom objects derived from CacheDependency, or any combination of these.

The AggregateCacheDependency class differs from the CacheDependency class in that it allows you to associate multiple dependencies of different types with a single cached item. For example, if you create a page that imports data from a SQL Server database table and an XML file, you can create a SqlCacheDependency object to represent a dependency on the database table and a CacheDependency to represent the dependency on the XML file. Rather than making an Cache.Insert method call for each dependency, you can create an instance of the AggregateCacheDependency class with each dependency added to it. You can then use a single Insert call to make the page dependent on the AggregateCacheDependency instance.

The following code example uses the AggregateCacheDependency class to add a DataSet, named XMLDataSet, to the cache that is dependent on a text file and an XML file.

' 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)

          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()

          ' 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

             chngMsg.Text = "The dependency changed last at: " & aggDep.UtcLastModified.ToString()
          End If

          cacheMsg1.Text = "Dataset created explicitly"
          cacheMsg1.Text = "Dataset retrieved from cache"
        End If

          MyLiteral.Text = Source.Table.TableName
          MyDataGrid.DataSource = Source
      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."
          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:")

          ' Write arbitrary objects to the file as needed.
          sw.Write("Text added at:")
       End If
     End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

© 2016 Microsoft