MemoryCache 范围是会话还是应用程序范围? [英] Is MemoryCache scope session or application wide?

查看:29
本文介绍了MemoryCache 范围是会话还是应用程序范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP.NET 中使用 MemoryCache 并且运行良好.我有一个对象缓存了一个小时,以防止从存储库中提取新的数据.

I'm using MemoryCache in ASP.NET and it is working well. I have an object that is cached for an hour to prevent fresh pulls of data from the repository.

我可以看到缓存在调试中工作,而且一旦部署到服务器,在第一次调用并缓存对象后,后续调用的时间约为 1/5.

I can see the caching working in debug, but also once deployed to the server, after the 1st call is made and the object is cached subsequent calls are about 1/5 of the time.

但是我注意到每个 客户端调用(仍然在 1 小时窗口内 - 实际上只是一两分钟后)似乎对我的服务进行了第一个调用(正在执行缓存)几乎与缓存数据之前的原始调用一样长.

However I'm noticing that each new client call (still inside that 1 hour window - in fact just a minute or 2 later) seems to have the 1st call to my service (that is doing the caching) taking almost as long as the original call before the data was cached.

这让我开始怀疑 - 是 MemoryCache 会话特定的,每个新客户端进行调用都存储它自己的缓存,或者是其他原因导致第一个调用需要很长时间甚至之后我知道数据已被缓存?

This made me start to wonder - is MemoryCache session specific, and each new client making the call is storing it's own cache, or is something else going on to cause the 1st call to take so long even after I know the data has been cached?

推荐答案

来自 MSDN:

Cache 和 MemoryCache 类之间的主要区别是已更改 MemoryCache 类以使其可用于 .NET不是 ASP.NET 应用程序的框架应用程序.例如,MemoryCache 类不依赖于 System.Web 程序集.另一个区别是您可以创建多个实例MemoryCache 类用于同一应用程序和相同的AppDomain 实例.

The main differences between the Cache and MemoryCache classes are that the MemoryCache class has been changed to make it usable by .NET Framework applications that are not ASP.NET applications. For example, the MemoryCache class has no dependencies on the System.Web assembly. Another difference is that you can create multiple instances of the MemoryCache class for use in the same application and in the same AppDomain instance.

阅读并在反射代码中进行一些调查,很明显MemoryCache 只是一个简单的类.您可以使用 MemoryCache.Default 属性来(重新)使用相同的实例,或者您可以根据需要构建尽可能多的实例(尽管建议尽可能少).

Reading that and doing some investigation in reflected code it is obvious that MemoryCache is just a simple class. You can use MemoryCache.Default property to (re)use same instance or you can construct as many instances as you want (though recommended is as few as possible).

所以基本上答案就在您的代码中.
如果您使用 MemoryCache.Default,那么只要您的应用程序池存在,您的缓存就会存在.(只是提醒您,默认的应用程序池空闲超时时间为 20 分钟,不到 1 小时.)

So basically the answer lies in your code.
If you use MemoryCache.Default then your cache lives as long as your application pool lives. (Just to remind you that default application pool idle time-out is 20 minutes which is less than 1 hour.)

如果你使用 new MemoryCache(string, NameValueCollection) 创建它,那么上面提到的注意事项加上你创建实例的上下文,也就是说,如果你在控制器内创建你的实例(我希望情况并非如此)那么您的缓存只为一个请求而存在

If you create it using new MemoryCache(string, NameValueCollection) then the above mentioned considerations apply plus the context you create your instance in, that is if you create your instance inside controller (which I hope is not the case) then your cache lives for one request

很遗憾我找不到任何引用,但是... MemoryCache 不保证根据您指定的缓存策略保存数据.特别是如果您运行应用的机器内存紧张,您的缓存可能会被丢弃.

It's a pity I can't find any references, but ... MemoryCache does not guarantee to hold data according to a cache policy you specify. In particular if machine you're running your app on gets stressed on memory your cache might be discarded.

如果您仍然无法弄清楚缓存项早期失效的原因是什么,您可以利用 RemoveCallback,调查item失效的原因.

If you still have no luck figuring out what's the reason for early cache item invalidation you could take advantage of RemoveCallback and investigate what is the reason of item invalidation.

这篇关于MemoryCache 范围是会话还是应用程序范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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