Page.Cache 属性

定义

获取与该页驻留的应用程序关联的 Cache 对象。

[System.ComponentModel.Browsable(false)]
public System.Web.Caching.Cache Cache { get; }

属性值

与该页的应用程序关联的 Cache

属性

例外

未创建 Cache 的实例。

示例

下面的代码示例使用 Page.Cache 属性将两个整数的总和插入到 对象中System.Web.Caching.Cache。 然后,它使用 Cache.Get 方法检索值,并将其 Label 写入 Web 服务器控件。

// This is a simple page that demonstrates how to place a value
// in the cache from a page, and one way to retrieve the value.
// Declare two constants, myInt1 and myInt2 and set their values
// and declare a string variable, myValue.
const int myInt1 = 35;
const int myInt2 = 77;
string myValue;

// When the page is loaded, the sum of the constants
// is placed in the cache and assigned a key, key1.
void Page_Load(Object sender,  EventArgs arg) {
  Cache["key1"] = myInt1 + myInt2;

}

// When a user clicks a button, the sum associated
// with key1 is retrieved from the Cache using the
// Cache.Get method. It is converted to a string
// and displayed in a Label Web server control.
void CacheBtn_Click(object sender, EventArgs e) {
   if (Cache["key1"] == null) {
      myLabel.Text = "That object is not cached.";
   }
   else {
      myValue = Cache.Get("key1").ToString();
      myLabel.Text = myValue;
   }
}

注解

应用程序的 Cache 对象允许存储和检索后续请求的任意数据。 缓存不专门与页面或用户会话相关联。 它主要用于增强应用程序性能。 有关详细信息,请参阅 缓存应用程序数据。 有关应用程序缓存和页面输出缓存之间的差异的详细信息,请参阅 ASP.NET 缓存概述

适用于

产品 版本
.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

另请参阅