使用System.Web.Caching.Cache

Riz*_*Riz 15 c# asp.net caching

我正在尝试使用缓存,但得到以下错误.如何正确使用缓存?

protected void Page_Load(object sender, EventArgs e) {
x = System.DateTime.Now.ToString();
 if (Cache["ModifiedOn"] == null) { // first time so no key/value in Cache
    Cache.Insert("ModifiedOn", x); // inserts the key/value pair "Modified On", x            
 }
 else { // Key/value pair already exists in the cache
     x = Cache["ModifiedOn"].ToString();
 } }
Run Code Online (Sandbox Code Playgroud)

'System.Web.Caching.Cache'是'type',但用作'变量'

cod*_*net 43

System.Web.Caching.Cache:这是.NET缓存的实现.

System.Web.HttpContext.Current.Cache:这是该实现的实例,它位于应用程序域中.

如果你不在aspx页面后面的代码中,我想你想使用第二个.如果您位于aspx页面后面的代码中,请使用Cache.

您也可以Page.Cache.Insert直接使用System.Caching.Cache通过页面对象引用.所有这些都指向同一个应用程序缓存,它对所有用户都是全局的.

  • 而不是直接使用"缓存",只需使用"System.Web.HttpContext.Current.Cache".您可能需要添加对system.web程序集的引用,具体取决于您的库. (2认同)