CacheDependency 類別

定義

在儲存於 ASP.NET 應用程式的 Cache 物件中的項目以及檔案、快取索引鍵、這其中任一個的陣列或另一個 CacheDependency 物件之間建立相依性關係。Establishes a dependency relationship between an item stored in an ASP.NET application's Cache object and a file, cache key, an array of either, or another CacheDependency object. CacheDependency 類別會監視相依性關係,如此便能在其中任一個變更時,自動移除快取的項目。The CacheDependency class monitors the dependency relationships so that when any of them changes, the cached item will be automatically removed.

public ref class CacheDependency sealed : IDisposable
public ref class CacheDependency : IDisposable
public sealed class CacheDependency : IDisposable
public class CacheDependency : IDisposable
type CacheDependency = class
    interface IDisposable
Public NotInheritable Class CacheDependency
Implements IDisposable
Public Class CacheDependency
Implements IDisposable
繼承
CacheDependency
衍生
實作

範例

下列程式碼範例將示範如何使用 HasChanged 屬性,來判斷 CacheDependency 自從上一次要求專案之後是否已變更 CacheThe following code example demonstrates using the HasChanged property to determine whether a CacheDependency changed since the previous request for an item in the Cache. dt在參數中傳遞的值 start 會設定為 DateTime.NowThe dt value passed in the start parameter is set to DateTime.Now.

// Insert the cache item.
CacheDependency dep = new CacheDependency(fileName, dt);
cache.Insert("key", "value", dep);

// Check whether CacheDependency.HasChanged is true.
if (dep.HasChanged)
  Response.Write("<p>The dependency has changed.");  
else Response.Write("<p>The dependency has not changed.");
' Insert the cache item.
Dim dep As New CacheDependency(fileName, dt)
myCache.Insert("key", "value", dep)

' Check whether CacheDependency.HasChanged is true.
If dep.HasChanged Then
   Response.Write("<p>The dependency has changed.")
Else
   Response.Write("<p>The dependency has not changed.")
End If 

備註

如果您將專案加入至與另一個物件(例如檔案或檔案陣列)相依的快取中,則當物件變更時,會自動從快取中移除相依專案。If you add an item to the cache that is dependent on another object, such as a file or array of files, the dependent item is automatically removed from the cache when the object changes. 例如,假設您 DataSet 根據 XML 檔案中的資料來建立物件。For example, suppose you create a DataSet object based on data in an XML file. 您可以 DataSet 使用會使相依于 XML 檔案的物件,將加入至快取 CacheDependency DataSetYou can add the DataSet to the cache with a CacheDependency object that makes the DataSet dependent on the XML file. 如果 XML 檔案變更,則 DataSet 會從快取中移除。If the XML file changes, the DataSet is removed from the cache.

您可以使用和方法,將具有相依性的專案新增至應用程式的快取 Add Cache.InsertYou can add items with dependencies to your application's cache with the Add and Cache.Insert methods. 您不能使用 Item[] 屬性將專案新增至具有相依性的快取。You cannot use the Item[] property to add items to the cache with dependencies.

若要設定相依性,您可以建立類別的實例 CacheDependency ,並指定該專案所依存的檔案、索引鍵或目錄,然後將相依性傳遞至 AddCache.Insert 方法。To set up a dependency, you create an instance of the CacheDependency class, specifying the files, keys, or directories on which the item is dependent, and then pass the dependency to the Add or Cache.Insert method. CacheDependency實例可以代表單一檔案或目錄、檔案或目錄的陣列,或是檔案或目錄的陣列,以及快取索引鍵的陣列 (這些代表儲存在物件) 中的其他專案 CacheThe CacheDependency instance can represent a single file or directory, an array of files or directories, or an array of files or directories along with an array of cache keys (these represent other items stored in the Cache object).

建構函式

CacheDependency()

初始化 CacheDependency 類別的新執行個體。Initializes a new instance of the CacheDependency class.

CacheDependency(String)

初始化 CacheDependency 類別的新執行個體,這執行個體可監視檔案或目錄的變更。Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.

CacheDependency(String, DateTime)

初始化 CacheDependency 類別的新執行個體,這執行個體可監視檔案或目錄的變更。Initializes a new instance of the CacheDependency class that monitors a file or directory for changes.

CacheDependency(String[])

初始化 CacheDependency 類別的新執行個體,其可監視檔案或目錄之路徑陣列的變更。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes.

CacheDependency(String[], DateTime)

初始化 CacheDependency 類別的新執行個體,其可監視檔案或目錄的路徑陣列之變更,並指定變更監視開始的時間。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories) for changes and specifies a time when change monitoring begins.

CacheDependency(String[], String[])

初始化 CacheDependency 類別的新執行個體,其可監視檔案或目錄的路徑陣列、快取索引鍵的陣列或這兩者的變更。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes.

CacheDependency(String[], String[], CacheDependency)

初始化 CacheDependency 類別的新執行個體,其可監視檔案或目錄的路徑陣列、快取索引鍵的陣列或這兩者的變更。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes. 它也會使本身相依於 CacheDependency 類別的個別執行個體。It also makes itself dependent upon a separate instance of the CacheDependency class.

CacheDependency(String[], String[], CacheDependency, DateTime)

初始化 CacheDependency 類別的新執行個體,其可監視檔案或目錄的路徑陣列、快取索引鍵的陣列或這兩者的變更。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes. 它也會使本身相依於 CacheDependency 類別的另一個執行個體和開始監控變更的時間。It also makes itself dependent upon another instance of the CacheDependency class and a time when the change monitoring begins.

CacheDependency(String[], String[], DateTime)

初始化 CacheDependency 類別的新執行個體,其可監視檔案或目錄的路徑陣列、快取索引鍵的陣列或這兩者的變更。Initializes a new instance of the CacheDependency class that monitors an array of paths (to files or directories), an array of cache keys, or both for changes.

屬性

HasChanged

取得值,指出 CacheDependency 是否已經變更。Gets a value indicating whether the CacheDependency object has changed.

UtcLastModified

取得上次變更相依性的時間。Gets the time when the dependency was last changed.

方法

DependencyDispose()

釋放由 CacheDependency 類別及任何衍生自 CacheDependency 的類別所使用的資源。Releases the resources used by the CacheDependency class and any classes that derive from CacheDependency.

Dispose()

釋放 CacheDependency 物件所使用的資源。Releases the resources used by the CacheDependency object.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
FinishInit()

完成 CacheDependency 物件的初始化。Completes initialization of the CacheDependency object.

GetFileDependencies()

取得檔案相依性。Gets the file dependencies.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
GetUniqueID()

擷取 CacheDependency 物件的唯一識別項。Retrieves a unique identifier for a CacheDependency object.

ItemRemoved()

已在移除受監視的快取項目時呼叫。Called when a monitored cache entry is removed.

KeepDependenciesAlive()

針對相依於這個項目的每個快取項目,更新上次存取時間。Updates the last access time of every cache item that depends on this item.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

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

告知基底 CacheDependency 物件,由衍生的 CacheDependency 類別所表示的相依性已經變更。Notifies the base CacheDependency object that the dependency represented by a derived CacheDependency class has changed.

SetCacheDependencyChanged(Action<Object,EventArgs>)

加入 Action 方法來處理在變更此相依性時通知有興趣的對象。Adds an Action method to handle notifying interested party in changes to this dependency.

SetUtcLastModified(DateTime)

標記上次變更相依性的時間。Marks the time when a dependency last changed.

TakeOwnership()

允許第一位使用者宣告這個相依性的獨佔擁有權。Allows the first user to declare exclusive ownership of this dependency.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱