MemoryCache Empty : 设置后返回 null [英] MemoryCache Empty : Returns null after being set

查看:21
本文介绍了MemoryCache Empty : 设置后返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用新的 .NET 4 System.Runtime.Caching MemoryCache 的 MVC 3 应用程序中遇到问题.我注意到,在一段看似不可预测的时间之后,它会停止缓存内容,并且表现得好像它是空的.考虑一下我直接从 ASP.NET MVC 中的测试视图中获取的这段代码:

I have a problem with an MVC 3 application that is using the new .NET 4 System.Runtime.Caching MemoryCache. I notice that after a seemingly unpredictable time, it stops caching stuff, and acts like it's empty. Consider this bit of code that I took straight from a test View in ASP.NET MVC:

MemoryCache.Default.Set("myname","fred", new CacheItemPolicy() { SlidingExpiration = new TimeSpan(0,5,0) });
Response.Write(MemoryCache.Default["myname"]);

当它工作时,可以预见fred"会被打印出来.但是,当问题开始出现时,尽管使用了Set(),但MemoryCache.Default["myname"] 的值为空.我可以通过在 Response.Write() 行上设置断点并使用立即窗口直接设置和读取缓存来证明这一点 - 它只是不会设置它并保持为空!让它再次工作的唯一方法是导致 AppDomain 回收.

When it's working, predictably "fred" gets printed. However, when the problem starts to occur, despite the Set(), the value of MemoryCache.Default["myname"] is null. I can prove this by setting a breakpoint on the Response.Write() line and directly setting and reading from the cache using the Immediate Window - It just won't set it and stays null! The only way to get it working again then is to cause an AppDomain recycle.

有趣的是,当应用程序正常工作时,我可以通过中断 Response.Write() 行并运行 MemoryCache.Default.Dispose() 来引发问题发生.在那之后, MemoryCache.Default 本身不为空(为什么是这样?),但不会保存任何设置在它上面.它不会导致任何错误,但不会保存任何内容.

Intriguingly I can provoke the problem into occurring when the app is working normally by breaking on the Response.Write() line and running MemoryCache.Default.Dispose(). After that, MemoryCache.Default is not null itself (why is this?), but won't save anything set on it. It doesn't cause any errors, but just won't save anything.

有人可以验证并解释一下吗?我相信我已经发现,当应用程序停止自行运行时,某些东西正在处理 MemoryCache.Default,但不是我!

Can anybody verify this and explain? As I believe I have discovered, when the app stops working on its own, something is Disposing MemoryCache.Default, but it's not me!

更新

好吧,我现在厌倦了这个问题!CLRProfiler 似乎不适用于 MVC 3.SciTech 的 CLR 工具很好 - RedGate ANTS 也很好.但他们只告诉我,MemoryCache 对象正在被某事处理!我还证明(通过时间戳打印)我的页面上应该缓存的 PartialView(由 OutputCacheAttribute 指定)在几分钟后停止缓存 - 每次调用页面时它都会开始刷新.为了澄清环境,我直接在运行 Win 7 Ultimate 的开发工作站上的 IIS 7.5 服务器上运行.上面提到的内存工具表明,就游戏中的对象而言,我只使用了大约 9mb 的内存.

Well, I'm sick of this prob now! CLRProfiler doesn't seem to work with MVC 3. SciTech's CLR tool was good - so was RedGate ANTS. But all they told me was that the MemoryCache object is being disposed by something! I also proved (via a timestamp print) that a PartialView on my page that should be cached (specified by OutputCacheAttribute) stops being cached after a few minutes - it starts refreshing with every call to the page. Just to clarify the environment, I am running directly on the IIS 7.5 server on my development workstation running Win 7 Ultimate. The memory tools mentioned above suggest I am only using about 9mb of memory in terms of objects in play.

无奈之下,我更改了缓存代码,首先搜索环境 HttpContext 以挂钩并使用其缓存功能(如果可用).早期测试表明这是可靠的,但感觉就像一个讨厌的黑客.

In desperation I have changed my caching code to first search for an ambient HttpContext to hook onto and use its Caching functionality, if one's available. Early tests show this is reliable, but it feels like a nasty hack.

我觉得 MemoryCache 和 OutputCache 不能保证与 MVC 3 一起使用...

Am getting the feeling that MemoryCache and OutputCache aren't warranted to work with MVC 3...

推荐答案

那么,这里有一些新闻.我们对此进行了调查,是的,这是 .NET 4 中的一个错误.

So, here's some news. We looked into this and YES, this is a bug in .NET 4.

好消息是它已在 .NET 4.5 中得到修复,因此如果可以,请将您的安装更新到 .NET 4.5 并且您很可靠.

The good news is that it was fixed in .NET 4.5, so if you can, update your installation to .NET 4.5 and you're solid.

另一个好消息是,此修复已向后移植到 .NET 4,并将作为 QFE(快速修复...您将应用的一次性修复)#578315.它在几天前被反向移植/修复,应该尽快推出.我会试着确定一个确切的日期,但很快.

The other good news it that this fix has been back-ported to .NET 4 and will be available as a QFE (Quick Fix...a one off fix you'll apply) #578315. It was backported/fixed just days ago and it should be out ASAP. I'll try to get an exact date, but it's soon.

另一个好消息是,在 QFE 之前,.NET 4 上有一个解决方法.解决方法很奇怪,但可以解除对您的阻止.

The other other good news is that there's a workaround for this on .NET 4 before the QFE. The workaround is weird, but it could unblock you.

using (ExecutionContext.SuppressFlow())     {
          // Create memory cache instance under disabled execution context flow
         return new YourCacheThing.GeneralMemoryCache(…);
}

希望这会有所帮助.

更新:修补程序是 http://support.microsoft.com/kb/2828843你可以在这里申请:https://support.microsoft.com/contactus/emailcontact.aspx?scid=sw;%5BLN%5D;1422

UPDATE: The Hotfix is http://support.microsoft.com/kb/2828843 and you can request it here: https://support.microsoft.com/contactus/emailcontact.aspx?scid=sw;%5BLN%5D;1422

这篇关于MemoryCache Empty : 设置后返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆