使用的MemoryCache的多个实例 [英] Using multiple instances of MemoryCache

查看:1102
本文介绍了使用的MemoryCache的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 System.Runtime.Caching 命名空间的缓存功能添加到我的应用程序,并可能需要使用缓存在几个地方,并在不同的上下文。 要做到这一点,我想用几个实例的MemoryCache

I'd like to add caching capabilities to my application using the System.Runtime.Caching namespace, and would probably want to use caching in several places and in different contexts. To do so, I want to use several MemoryCache instances.

不过,我看到 的,使用多个实例的MemoryCache是​​不鼓励的:

However, I see here that using more than one instance of MemoryCache is discouraged:

的MemoryCache不是单身,但你应该创建只有几个或可能只有一个的MemoryCache实例,code表示缓存的项目应该使用这些实例。

MemoryCache is not a singleton, but you should create only a few or potentially only one MemoryCache instance and code that caches items should use those instances.

如何将多个的MemoryCache实例影响到我的应用程序? 我觉得这很怪,因为在我看来,使用多个缓存在应用程序中是pretty的常见的场景。

How would multiple MemoryCache instances affect my application? I find this kind of weird because it seems to me that using multiple caches in an application is a pretty common scenario.

编辑:更具体地说,我有应该保持一个高速缓存为每个实例的类。我应避免使用的MemoryCache ,并寻找一个不同的缓存解决方案?使用的MemoryCache 在这种情况下认为是不好的,如果是这样,为什么?

More specifically, I have a class that should keep a cache for each instance. Should I avoid using MemoryCache and look for a different caching solution? Is using MemoryCache in this situation considered bad, and if so, why?

推荐答案

最近,我通过这个自己去为好。考虑到一个内存中缓存会过程中的具体实在没有好处有多个(不跨网站或本地的业务应用程序或多个服务器的多个实例共享)的MemoryCache 除了实例为code组织方面的原因(这可以通过其它方式来实现)。

I recently went through this myself as well. Considering an in memory cache will be process specific (not shared across multiple instances of a website or native business app or multiple servers) there is really no benefit to having multiple MemoryCache instances except for code organizational reasons (which can be achieved in other ways).

的存储器高速缓存的目的是单独使用,因为它的存储器管理能力大多。除了性能计数器(其中确实有一些开销)中的MemoryCache也能当用完分配的内存到期的项目。

The Memory cache is intended to be used alone mostly because of its memory management capabilities. In addition to the performance counters (which do have some overhead) the MemoryCache is also able to expire items when it runs out of allocated memory.

如果缓存的当前实例超出内存设置的限制   由CacheMemoryLimit属性,缓存实现消除   缓存条目。在应用程序中的每个缓存实例可以使用   内存由CacheMemoryLimit属性指定的金额。

If the current instance of the cache exceeds the limit on memory set by the CacheMemoryLimit property, the cache implementation removes cache entries. Each cache instance in the application can use the amount of memory that is specified by the CacheMemoryLimit property.

从<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.cachememorylimit.aspx">MemoryCache.CacheMemoryLimit物业

通过使用的MemoryCache只有一个实例它可以有效地应用在整个应用程序实例这个内存管理。到期整个应用程序的至少重要的项目。这可确保最大内存使用,而不会超出你的硬件功能。通过限制任何一个的MemoryCache的范围(比如一类的一个实例),它不能有效地为您的应用程序内存管理(因为它无法看到的一切)。如果所有这些缓存的都是忙,你可能有一个更难的时间来管理内存和它永远不会是几乎一样有效。

By using only one instance of the MemoryCache it can apply this memory management efficiently across the entire application instance. Expiring the least important items across the entire application. This ensures maximum memory use, without exceeding your hardware capabilities. By limiting the scope of any one MemoryCache (like to one instance of a class) it can no longer effectively manage memory for your application (as it can't "see" everything). If all of these cache's were "busy" you may have a harder time managing memory and it will never be nearly as efficient.

这是在没有专用服务器的奢侈品应用尤为敏感。想象一下,你在哪里,你只分配了150MB RAM(普通便宜$ 10 /月的主机),你需要依靠你的缓存来使用,以最大不超过其共享服务器上运行你的应用程序。如果超出此内存使用你的应用程序池将被回收利用和您的应用程序失去所有的内存缓存! (共同主办便宜的做法)同样可以适用于托管在内部一些共同的企业服务器上的非Web应用程序。同样的协议,你被告知不能霸占所有的内存的机器上,并以和平方式与业务应用一些其他线路并存。

This is particularly sensitive in applications which don't have the luxury of a dedicated server. Imagine you are running your app on a shared server where you've only been allocated 150mb RAM (common cheap $10/month hosting) you need to count on your cache to use that to the max without exceeding it. If you exceed this memory usage your app pool will be recycled and your app loses all in memory caches! (common cheap hosting practice) The same could apply to a non-web app hosted in house on some shared corporate server. Same deal, you're told not to hog all the memory on that machine and to peacefully co-exist with some other line of business apps.

这是内存的限制,应用程序池回收,失去缓存的是一个共同的阿喀琉斯治愈为网络应用程序。当应用程序是他们最忙的,他们重新最常因超出内存分配,失去所有的缓存条目,并为此做了应该被缓存在第一时间最多的工作再取东西。这意味着应用程序实际上失去性能,最大负​​载,而不是获得。

That memory-limit, app pool recycle, lose caches thing is a common "Achilles heal" to web apps. When the apps are their busiest, they reset the most often due to exceeding memory allocations, losing all cache entries and therefor doing the most work re-fetching stuff that should have been cached in the first place. Meaning the app actually loses performance at max load instead of gaining.

我知道的MemoryCache是​​的System.Web.Caching.Cache实施非Web特定版本,但是这说明背后缓存实现的逻辑。同样的逻辑可以在非web项目中应用,如果您还没有专用的硬件。请记住,如果你的缓存强制机器开始做页面文件交换,然后缓存再也没有任何速度比缓存到磁盘上。你永远需要的地方的限制,即使该限制为2GB或东西。

I know MemoryCache is the non-web specific version of System.Web.Caching.Cache implementation, but this illustrates the logic behind cache implementation. The same logic can apply in a non-web project if you don't have exclusive use of the hardware. Remember if your cache forces the machine to start doing pagefile swaps then your cache is no longer any faster than caching on disk. You'll always want a limit somewhere, even if that limit is 2gb or something.

在我的情况下,读了这个之后,我切换到我的应用程序使用一个公共静态的MemoryCache',我只是隔离缓存的项目由他们缓存键。例如,如果你想在缓存每个实例,你可以有喜欢的东西,如实例 - {实例Id} {-resourceName-} RESOURCEID一个缓存键。把它看成是名间距缓存条目。

In my case after reading up about this, I switched to using one 'public static MemoryCache' in my app and I simply segregated cached items by their cache keys. For example if you want to cache on a per instance you could have a cache key like something like "instance-{instanceId}-resourceName-{resourceId}". Think of it as name spacing your cache entries.

希望帮助!

这篇关于使用的MemoryCache的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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