使用英语阅读

通过


Cache.Item[String] 属性

定义

获取或设置指定键处的缓存项。

public object this[string key] { get; set; }

参数

key
String

表示缓存项的键的 String 对象。

属性值

指定的缓存项。

示例

以下示例使用 Item 属性检索与 Key1 键关联的缓存对象的值。 然后, HttpResponse.Write 它使用 方法将值和介绍性文本以及 B HTML 元素写入 Web 窗体页面。

Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");

以下示例演示如何使用此属性将文本框的值插入缓存中。

private void cmdAdd_Click(Object objSender, EventArgs objArgs)
{
    if (txtName.Text != "")
    {
        // Add this item to the cache.
        Cache[txtName.Text] = txtValue.Text;
    }
}

注解

可以使用此属性检索指定缓存项的值,或将项和键添加到缓存。 使用 Item[] 属性添加缓存项等效于调用 Cache.Insert 方法。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅