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

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

问题描述

我在ASP.NET中使用的MemoryCache 和它运作良好。我有一个缓存一个小时prevent数据新鲜拉从资源库中的对象。

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小时的窗口内 - 实际上只是一分钟或2晚)似乎已经到了我的服务第一个呼叫(即做缓存)以几乎只要原始呼叫的数据被缓存之前

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 :

缓存和的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.

读取和做的反射$ C $一番调查c那么它显然的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).

所以基本上,答案就在你的code。结果
如果你使用 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.)

如果您在使用创建新的MemoryCache(字符串的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.

如果您还没有运气搞清楚什么是早期的缓存项失效的原因,你可以利用<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.caching.cacheitempolicy.removedcallback.aspx\"><$c$c>RemoveCallback并探讨什么是项目失效的原因。

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天全站免登陆