語言

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 類別,將 DataSet一個名為 XMLDataSet、 的快取加入,該快取依賴於文字檔與 XML 檔案。

   ' 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 例。

建構函式

名稱 Description
AggregateCacheDependency()

初始化 AggregateCacheDependency 類別的新執行個體。

屬性

名稱 Description
HasChanged

會得到一個值,表示物件是否 CacheDependency 改變。

(繼承來源 CacheDependency)
UtcLastModified

會顯示依賴性最後變更的時間。

(繼承來源 CacheDependency)

方法

名稱 Description
Add(CacheDependency[])

在物件中加入一組 CacheDependency 物件 AggregateCacheDependency 。

DependencyDispose()

釋放該類別及源CacheDependency自 的類別所使用的CacheDependency資源。

(繼承來源 CacheDependency)
Dispose()

釋放物件 CacheDependency 所使用的資源。

(繼承來源 CacheDependency)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FinishInit()

完成物件初始化 CacheDependency 。

(繼承來源 CacheDependency)
GetFileDependencies()

取得檔案相依關係。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
GetUniqueID()

取得物件的唯一識別碼 AggregateCacheDependency 。

ItemRemoved()

當被監控的快取項目被移除時,會被呼叫。

(繼承來源 CacheDependency)
KeepDependenciesAlive()

更新每個依賴該快取項目的最後存取時間。

(繼承來源 CacheDependency)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
NotifyDependencyChanged(Object, EventArgs)

通知基底 CacheDependency 物件派生 CacheDependency 類別所代表的相依關係已變更。

(繼承來源 CacheDependency)
SetCacheDependencyChanged(Action<Object,EventArgs>)

新增一個動作方法,用來通知相關方對此相依變更的通知。

(繼承來源 CacheDependency)
SetUtcLastModified(DateTime)

標記依賴性最後一次變更的時間點。

(繼承來源 CacheDependency)
TakeOwnership()

允許第一個使用者宣告對此依賴的專屬擁有權。

(繼承來源 CacheDependency)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於