簡體   English   中英

奇怪的MemoryCache類問題

[英]Weird MemoryCache class issue

我在Windows 7上的VS 2010 Ultimate上運行,並嘗試使用MemoryCache(System.Runtime.Caching),由於某種原因,當方法結束時緩存會立即清除,而當我再次重新運行該方法時,它正在嘗試創建一個新的。 這是我從MSDN文檔使用的代碼:

           ObjectCache cache = MemoryCache.Default;
    string fileContents = cache["filecontents"] as string;

    if (fileContents == null)
    {
        CacheItemPolicy policy = new CacheItemPolicy();

        List<string> filePaths = new List<string>();
        filePaths.Add("c:\\Windows\\Enterprise.xml");

//      policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
        policy.Priority = CacheItemPriority.NotRemovable;
        policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(3600);

        // Fetch the file contents.
        fileContents =
            File.ReadAllText("c:\\Windows\\Enterprise.xml");

        cache.Set("filecontents", fileContents, policy);
    }

        Console.WriteLine(fileContents);

我在Console Main方法中有此代碼。

令人驚訝的是,我有一個包裝的C#4.0程序集,該程序集是我從QTP中使用的,並且運行得非常好。 緩存保留在QTP中的每次運行中。

請幫忙。

MemoryCache的生存期與Visual Studio完全無關(您在注釋中提到“ Visual Studio IDE仍處於打開狀態”。

緩存僅在程序運行時可用,因此每次停止並重新啟動應用程序時,緩存實際上將為空。 更具體地說,它位於AppDomain的上下文中,該上下文在您啟動應用程序時創建,並在應用程序退出時銷毀。

僅當您的應用程序運行一個小時以上時,該策略才會生效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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