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)

适用于

另请参阅