Cache.Item[String] 属性


获取或设置指定键处的缓存项。Gets or sets the cache item at the specified key.

 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ key); void set(System::String ^ key, System::Object ^ value); };
public object this[string key] { get; set; }
member this.Item(string) : obj with get, set
Default Public Property Item(key As String) As Object



表示缓存项的键的 String 对象。A String object that represents the key for the cache item.


指定的缓存项。The specified cache item.


下面的示例使用Item属性检索Key1与键关联的缓存对象的值。The following example uses the Item property to retrieve the value of a cached object associated with the Key1 key. 然后, 它使用HttpResponse.Write方法将值、介绍性文本和 B HTML 元素写入 Web 窗体页。It then uses the HttpResponse.Write method to write the value and introductory text and the B HTML element to a Web Forms page.

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

下面的示例演示如何使用此属性将文本框的值插入缓存中。The following example demonstrates using this property to insert the value of a text box into the cache.

private void cmdAdd_Click(Object objSender, EventArgs objArgs)
    if (txtName.Text != "")
        // Add this item to the cache.
        Cache[txtName.Text] = txtValue.Text;
Private Sub cmdAdd_Click(objSender As Object, objArgs As EventArgs)
  If txtName.Text <> "" Then
    ' Add this item to the cache.
  Cache(txtName.Text) = txtValue.Text
  End If
End Sub


您可以使用此属性检索指定缓存项的值, 或者将项和项添加到缓存中。You can use this property to retrieve the value of a specified cache item, or to add an item and a key for it to the cache. 使用Item[String]属性添加缓存项等效于Cache.Insert调用方法。Adding a cache item using the Item[String] property is equivalent to calling the Cache.Insert method.