簡體   English   中英

如何在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM