![](/img/trans.png)
[英]How to store a stored procedure result in cache in asp.net System.Web.Caching
[英]How to use System.Web.Caching in asp.net?
我有一個看起來像這樣的課程:
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 Cache 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>;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.