CacheItem 類別

定義

表示快取區中的個別快取項目。

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

範例

下列範例示範如何使用 CacheItem 類別,將檔案的內容儲存為快取專案。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Caching;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {
        ObjectCache cache = MemoryCache.Default;

        CacheItem fileContents = cache.GetCacheItem("filecontents");

        if (fileContents == null)
        {
            CacheItemPolicy policy = new CacheItemPolicy();

            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
            string fileData = File.ReadAllText(cachedFilePath);

            fileContents = new CacheItem("filecontents", fileData);

            cache.Set(fileContents, policy);
        }
        Label1.Text = (fileContents.Value as string);
    }
}
Imports System.Runtime.Caching
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    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 CacheItem = cache.GetCacheItem("filecontents")

        If fileContents Is Nothing Then
            Dim policy As New CacheItemPolicy()

            Dim filePaths As New List(Of String)()

            Dim CachedFilePaths As String = Server.MapPath("~") & "\cacheText.txt"

                filePaths.Add(CachedFilePaths)

            policy.ChangeMonitors.Add(New HostFileChangeMonitor(filePaths))

            ' Fetch the file contents 
            Dim fileData As String = File.ReadAllText(CachedFilePaths)

            fileContents = New CacheItem("filecontents", fileData)

            cache.Set(fileContents, policy)
        End If
        Label1.Text = TryCast(fileContents.Value, String)
    End Sub
End Class

備註

類別 CacheItem 提供快取專案的邏輯標記法,其可使用 屬性來包含區域 RegionName 。 在預設 ASP.NET 快取實作中,快取專案是索引鍵/值組。

快取中的專案不是 CacheItem 實例。 相反地,快取提供者可以使用任何方便的內部格式來儲存快取專案。 不過,快取 API 要求快取提供者能夠將快取專案 CacheItem 轉換成實例 (,反之亦然) 。

自訂快取實作可以繼承自 CacheItem 類別,提供有關快取專案的其他資訊。

給繼承者的注意事項

類別 ObjectCache 具有支援新增、擷取和更新快取資料的方法,而且所有這些方法都有支援 類別的多 CacheItem 載。 因此,自訂快取實作可以建立擴充 CacheItem 類別,並使用該擴充類別搭配現有的 ObjectCache 快取專案 API。

建構函式

CacheItem(String)

使用指定的快取項目索引鍵,初始化新的 CacheItem 執行個體。

CacheItem(String, Object)

使用指定的快取項目索引鍵和值,初始化新的 CacheItem 執行個體。

CacheItem(String, Object, String)

使用指定的快取項目索引鍵、值和區域,初始化新的 CacheItem 執行個體。

屬性

Key

取得或設定 CacheItem 執行個體的唯一識別項。

RegionName

取得或設定快取中包含 CacheItem 項目之區域的名稱。

Value

取得或設定 CacheItem 執行個體的資料。

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於

另請參閱