qua*_*els 17 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 settings = Cache[cacheKey] as Dictionary<string, bool>; // error on this line
// ...etc...
return settings
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是这不会编译.编译器说Cache不能按我这样做的方式使用.这是消息:
'System.Web.Caching.Cache' is a 'type' but is used like a 'variable'
Run Code Online (Sandbox Code Playgroud)
这让我感到困惑.我搜索了ASP.NET Cache API,并找到了许多以Cache这种方式使用的例子.以下是其中一个例子:
// http://www.4guysfromrolla.com/articles/100902-1.aspx
value = Cache("key")
- or -
value = Cache.Get("key")
Run Code Online (Sandbox Code Playgroud)
当我尝试使用时,Cache.Get()我得到另一个错误,说它不是静态方法.
显然我需要初始化一个实例Cache.这是使用此API的正确方法吗?后续问题是,缓存信息是否会持续存在于实例中?
谢谢你的帮助.
zim*_*nen 23
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>;
Run Code Online (Sandbox Code Playgroud)