C# 如何在asp.net中使用System.Web.Caching?
我有一个类似这样的类:C# 如何在asp.net中使用System.Web.Caching?,c#,asp.net,caching,asp.net-4.5,C#,Asp.net,Caching,Asp.net 4.5,我有一个类似这样的类: 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 s
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
}
}
这使我困惑。我在谷歌上搜索了ASP.NET缓存API,发现了许多这样使用Cache
的例子。以下是其中一个例子:
// http://www.4guysfromrolla.com/articles/100902-1.aspx
value = Cache("key")
- or -
value = Cache.Get("key")
当我尝试使用Cache.Get()
时,我收到另一个错误,它不是静态方法
显然,我需要初始化缓存的一个实例。这是使用此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>;
var settings=System.Web.HttpRuntime.Cache[cacheKey]作为字典;
使用HttpRuntime.Cache
访问Cache
instance.Awesome。我喜欢有一个简单的解释!
var settings = System.Web.HttpRuntime.Cache[cacheKey] as Dictionary<string, bool>;