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 使用 類別,將 名為 XMLDataSet
的 新增 DataSet 至相依于文字檔和 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 實例。
建構函式
AggregateCacheDependency() |
初始化 AggregateCacheDependency 類別的新執行個體。 |
屬性
HasChanged |
取得值,指出 CacheDependency 是否已經變更。 (繼承來源 CacheDependency) |
UtcLastModified |
取得上次變更相依性的時間。 (繼承來源 CacheDependency) |