CacheItemPolicy 類別

定義

表示特定快取項目的一組收回和期限詳細資料。

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

範例

下列範例示範如何建立記憶體內部快取專案,以監視文本文件的路徑。 快取會 CacheItemPolicy 建立 物件,並將 屬性設定 AbsoluteExpiration 為在 60 秒後收回快取。

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實例包含可與快取專案相關聯的資訊。 例如,當快取專案即將從快取中移除時, CacheEntryUpdateArguments 對象會傳遞至回呼方法。 對象的 UpdatedCacheItemPolicy 屬性 CacheEntryUpdateArguments 可以傳遞實例的參考 CacheItemPolicy ,該實例可以包含快取專案的收回和到期詳細數據。

ObjectCache 類別中的MemoryCache某些方法會接受 CacheItemPolicy 實例來描述收回或到期原則。

給繼承者的注意事項

類型 CacheItemPolicy 未密封,讓自定義快取開發人員可以擴充它。

建構函式

CacheItemPolicy()

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

屬性

AbsoluteExpiration

取得或設定值,這個值指出在指定的時間點是否應收回快取項目。

ChangeMonitors

取得與快取項目關聯之 ChangeMonitor 物件的集合。

Priority

取得或設定用來決定是否收回快取項目的優先權設定。

RemovedCallback

取得或設定對 CacheEntryRemovedCallback 委派的參考,移除快取中的項目後會呼叫該委派。

SlidingExpiration

取得或設定值,指出是否應該清除已經有一段時間沒有存取過的快取項目。

UpdateCallback

取得或設定對 CacheEntryUpdateCallback 委派的參考,在快取區中移除快取項目前會呼叫該參考。

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於