如何在 asp.net 中使用 System.Web.Caching
本文关键字:System Web Caching asp net | 更新日期: 2023-09-27 18:33:00
>我有一个看起来像这样的类:
using System.Collections.Generic;
using System.Web.Caching;
public static class MyCache
{
private static string cacheKey = "mykey";
public static Dictionary<string, bool> GetCacheValue(bool bypassCache)
{
var settings = Cache[cacheKey] as Dictionary<string, bool>; // error on this line
// ...etc...
return settings
}
}
我遇到的问题是这不会编译。编译器说Cache
不能按照我的方式使用。消息如下:
'System.Web.Caching.Cache' is a 'type' but is used like a 'variable'
这让我感到困惑。我用谷歌搜索了 ASP.NET 缓存API,发现了许多以这种方式使用Cache
的例子。下面是其中一个示例:
// http://www.4guysfromrolla.com/articles/100902-1.aspx
value = Cache("key")
- or -
value = Cache.Get("key")
当我尝试使用Cache.Get()
时,我收到另一个错误,说它不是静态方法。
显然,我需要初始化Cache
的实例。这是使用此 API 的正确方法吗?后续问题是,缓存的信息是否跨实例持久存在?
感谢您的帮助。
>System.Web.Caching.Cache
是一个类 - 你会看到人们使用一个名为Cache
的属性,它是System.Web.Caching.Cache
的实例。 如果在提供 Cache
属性的类之外使用它,请使用 System.Web.HttpRuntime.Cache
访问它:
var settings = System.Web.HttpRuntime.Cache[cacheKey] as Dictionary<string, bool>;