CacheItemPolicy 類別

定義

表示特定快取項目的一組收回和期限詳細資料。Represents a set of eviction and expiration details for a specific cache entry.

public ref class CacheItemPolicy
public class CacheItemPolicy
type CacheItemPolicy = class
Public Class CacheItemPolicy
繼承
CacheItemPolicy

範例

下列範例顯示如何建立記憶體內部快取專案, 以監視文字檔的路徑。The following example shows how to create an in-memory cache item that monitors the path for a text file. 快取會建立CacheItemPolicy物件, 並AbsoluteExpiration將屬性設定為在60秒後收回快取。The cache creates a CacheItemPolicy object and sets the AbsoluteExpiration property to evict the cache after 60 seconds.

Protected Sub Button1_Click(ByVal sender As Object, _  
            ByVal e As System.EventArgs) Handles Button1.Click  
    Dim cache As ObjectCache = MemoryCache.Default  
    Dim fileContents As String = TryCast(cache("filecontents"), _  
            String)  
    If fileContents Is Nothing Then  
        Dim policy As New CacheItemPolicy()  
        policy.AbsoluteExpiration = _  
            DateTimeOffset.Now.AddSeconds(60.0)  
        Dim filePaths As New List(Of String)()  
     Dim cachedFilePath As String = Server.MapPath("~") & _  
         "\cacheText.txt"  
        filePaths.Add(cachedFilePath)  
        policy.ChangeMonitors.Add(New _  
            HostFileChangeMonitor(filePaths))  
  
        ' Fetch the file contents.  
        fileContents = File.ReadAllText(cachedFilePath)  
        cache.Set("filecontents", fileContents, policy)  
    End If  
    Label1.Text = fileContents  
End Sub  
protected void Button1_Click(object sender, EventArgs e)  
    {  
        ObjectCache cache = MemoryCache.Default;  
            string fileContents = cache["filecontents"] as string;  
            if (fileContents == null)  
            {  
                CacheItemPolicy policy = new CacheItemPolicy();  
                policy.AbsoluteExpiration =   
                    DateTimeOffset.Now.AddSeconds(60.0);  
  
                List<string> filePaths = new List<string>();  
             string cachedFilePath = Server.MapPath("~") +   
                 "\\cacheText.txt";  
             filePaths.Add(cachedFilePath);  
  
                policy.ChangeMonitors.Add(new   
                    HostFileChangeMonitor(filePaths));  
  
                // Fetch the file contents.  
                fileContents = File.ReadAllText(cachedFilePath);  
  
                cache.Set("filecontents", fileContents, policy);  
  
            }  
  
            Label1.Text = fileContents;  
        }  

備註

CacheItemPolicy實例包含可與快取專案建立關聯的資訊。A CacheItemPolicy instance contains information that can be associated with a cache entry. 例如, 當要從快取中移除快取專案時, CacheEntryUpdateArguments會將物件傳遞至回呼方法。For example, when a cache entry is about to be removed from the cache, a CacheEntryUpdateArguments object is passed to a callback method. CacheEntryUpdateArguments物件UpdatedCacheItemPolicy的屬性可以將參考CacheItemPolicy傳遞給實例, 其中可包含有關快取專案的收回和到期詳細資料。The UpdatedCacheItemPolicy property of the CacheEntryUpdateArguments object can pass a reference to a CacheItemPolicy instance that can include eviction and expiration details about the cache entry.

CacheItemPolicyMemoryCache 類別中的某些方法會接受實例,以描述收回或到期原則。ObjectCacheSome methods in the MemoryCache and ObjectCache classes accept a CacheItemPolicy instance to describe eviction or expiration policy.

給繼承者的注意事項

CacheItemPolicy類型未密封, 因此自訂快取開發人員可以加以擴充。The CacheItemPolicy type is unsealed so that custom cache developers can extend it.

建構函式

CacheItemPolicy()

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

屬性

AbsoluteExpiration

取得或設定值,這個值指出在指定的時間點是否應收回快取項目。Gets or sets a value that indicates whether a cache entry should be evicted at a specified point in time.

ChangeMonitors

取得與快取項目關聯之 ChangeMonitor 物件的集合。Gets a collection of ChangeMonitor objects that are associated with a cache entry.

Priority

取得或設定用來決定是否收回快取項目的優先權設定。Gets or sets a priority setting that is used to determine whether to evict a cache entry.

RemovedCallback

取得或設定對 CacheEntryRemovedCallback 委派的參考,移除快取中的項目後會呼叫該委派。Gets or sets a reference to a CacheEntryRemovedCallback delegate that is called after an entry is removed from the cache.

SlidingExpiration

取得或設定值,指出是否應該清除已經有一段時間沒有存取過的快取項目。Gets or sets a value that indicates whether a cache entry should be evicted if it has not been accessed in a given span of time.

UpdateCallback

取得或設定對 CacheEntryUpdateCallback 委派的參考,在快取區中移除快取項目前會呼叫該參考。Gets or sets a reference to a CacheEntryUpdateCallback delegate that is called before a cache entry is removed from the cache.

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

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

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
MemberwiseClone()

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

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於