CacheItem 類別

定義

表示快取區中的個別快取項目。Represents an individual cache entry in the cache.

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

範例

下列範例示範如何使用CacheItem類別, 將檔案的內容儲存為快取專案。The following example shows how to use the CacheItem class to store the contents of a file as a cache entry.

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

備註

類別提供快取專案的邏輯標記法, 可以RegionName使用屬性來包含區域。 CacheItemThe CacheItem class provides a logical representation of a cache entry, which can include regions by using the RegionName property. 在預設的 ASP.NET 快取執行中, 快取專案是索引鍵/值組。In the default ASP.NET cache implementation, a cache entry is a key/value pair.

快取中的專案不CacheItem是實例。Entries in the cache are not CacheItem instances. 相反地, 快取提供者可以用任何方便的內部格式來儲存快取專案。Instead, the cache provider can store cache entries in any internal format that is convenient. 不過, 快取 API 要求快取提供者能夠將快取專案轉換CacheItem成實例 (反之亦然)。However, the cache API requires cache providers to be able to convert cache entries into CacheItem instances (and vice versa).

自訂快取實作為可以CacheItem從類別繼承, 以提供有關快取專案的其他資訊。Custom cache implementations can inherit from the CacheItem class provide additional information about cache entries.

給繼承者的注意事項

類別具有支援新增、提取和更新快取資料的方法, 而且所有這些方法都具有CacheItem支援類別的多載。 ObjectCacheThe ObjectCache class has methods that support adding, fetching, and updating cache data, and all these methods have overloads that support the CacheItem class. 因此, 自訂快取執行可以建立擴充CacheItem類別, 並將該擴充類別與現有ObjectCache的 API 一起用於快取專案。Therefore, a custom cache implementation can create an extended CacheItem class and use that extended class together with the existing ObjectCache API for cache entries.

建構函式

CacheItem(String)

使用指定的快取項目索引鍵,初始化新的 CacheItem 執行個體。Initializes a new CacheItem instance using the specified key of a cache entry.

CacheItem(String, Object)

使用指定的快取項目索引鍵和值,初始化新的 CacheItem 執行個體。Initializes a new CacheItem instance using the specified key and a value of the cache entry.

CacheItem(String, Object, String)

使用指定的快取項目索引鍵、值和區域,初始化新的 CacheItem 執行個體。Initializes a new CacheItem instance using the specified key, value, and region of the cache entry.

屬性

Key

取得或設定 CacheItem 執行個體的唯一識別項。Gets or sets a unique identifier for a CacheItem instance.

RegionName

取得或設定快取中包含 CacheItem 項目之區域的名稱。Gets or sets the name of a region in the cache that contains a CacheItem entry.

Value

取得或設定 CacheItem 執行個體的資料。Gets or sets the data for a CacheItem instance.

方法

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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於

另請參閱