单个或多个MemoryCache实例 [英] Single vs multiple MemoryCache instances

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

问题描述

MemoryCache默认带有默认高速缓存,并且可以创建其他命名的高速缓存。

MemoryCache comes with a Default cache by default and additional named caches can be created.

隔离不同过程结果的高速缓存似乎有优势例如,可以将对索引的查询结果缓存在 IndexQueryResult缓存中,将数据库查询的结果缓存在 DatabaseQueryResult缓存中。

It seems like there might be advantages to isolating the caching of results of different processes in different instances For example, results of queries against an index could be cached in an "IndexQueryResult" cache and result of database queries in a "DatabaseQueryResult" cache. That's rather contrived but explains the principle.

一个缓存上的导致驱逐的内存压力是否会完全影响其他缓存? .net管理多个缓存的方式与管理一个缓存的方式有什么区别?

Does the memory pressure on one cache that results in evictions affect the other caches at all? Are there any differences in the way .Net manages multiple caches compared to how it manages one?

我是在浪费时间考虑多个缓存的想法,还是真的这样做的价值?

Am I wasting my time considering the idea of multiple caches, or is there real value in doing so?

推荐答案

我无法说出前几个问题,并且我很想听听以下问题的答案那些。但是,可以说,到目前为止,我们在产品中使用多个缓存的经验非常丰富。这是我看到的好处:

I can't speak to the first few questions, and I'm interested to hear answers to those. However, I can say that we've been had a good experience so far using multiple caches in our product. Here are the benefits I see:


  • 减少按键碰撞的机会:而不是提出某种确保没有两个单独的值以相同的键结尾的方案,我们可以简单地创建一个特定于给定存储库类型的缓存,并且知道只要该存储库类使用其对象唯一的键,我们就不会

  • 使用缓存逐出的精度更高:拥有特定缓存实例的存储库类型可以订阅系统级事件总线上的某些事件类型,以便它知道何时需要清除缓存的某些部分。如果幸运的话,它可以纯粹根据已发布事件的参数来确定要清除的条目的键。但是,通常不是这种情况,我们必须清除整个缓存或遍历所有缓存的值,以找出哪些值受已发布事件的影响。如果我们对系统中的所有数据类型使用单个缓存实例,则最终将爬网许多不相关的条目。通过使用单独的缓存,我们可以将搜索限制为该特定存储库负责填充的值。

  • Reduced chance of key collision: Rather than coming up with some kind of scheme to ensure that no two separate values end up with the same key, we can simply create a cache that's specific to a given repository type, and know that as long as that repository class uses keys unique to its objects, we won't have collisions.
  • Better precision with cache eviction: The repository type that "owns" a particular cache instance can subscribe to certain event types on a system-wide event bus, so that it knows when some parts of the cache need to be purged. If we're lucky, it can determine the keys of the entries to purge purely based on the arguments of the published event. However, this is often not the case, and we must either purge the entire cache or iterate through all the cached values to figure out which ones are affected by the published event. If we were using a single cache instance for all data types in our system, we would end up crawling through a lot of unrelated entries. By using separate caches, we can restrict our search to the values that this particular repository was responsible for populating.

关于第二点:我们还构建了一个UI,以公开系统中的所有缓存实例,并允许我们通过单击按钮清除其中的任何一个。当我们需要直接对数据库进行更改,并且需要系统无需重新启动服务器就可以获取这些更改时,这非常方便。同样,如果仅使用单个缓存,我们将无法达到如此精确的效果:我们必须清除系统范围内的所有缓存值,而不仅仅是清除与我们处理过的数据类型相关联的值。

Regarding the second point: we also built a UI to expose all the cache instances in the system, and allow us to purge any of them with the click of a button. This comes in handy when we need to make changes directly to the database, and need the system to pick up those changes without having to restart the server. Again, if we only used a single cache, we couldn't be nearly as precise: we'd have to purge all the cached values systemwide, instead of just the values associated with the data types that we tinkered with.

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

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