简体   繁体   English

Asp.Net Core 分布式缓存

[英]Asp.Net core distributed caching

I am currently using MemoryCache _cache = new MemoryCache(new MemoryCacheOptions());我目前正在使用MemoryCache _cache = new MemoryCache(new MemoryCacheOptions()); for caching some data from database that does not change so often, but it does change.用于从数据库中缓存一些不经常更改但确实会更改的数据。 And on create/update/delete of that data I do the refresh of the cache.在创建/更新/删除该数据时,我会刷新缓存。

This works fine, but the problem is that on production we will have few nodes, so when method for creating of record is called for instance, cache will be refreshed only on that node, not on other nodes, and they will have stale data.这工作正常,但问题是在生产中我们将有很少的节点,因此当调用创建记录的方法时,缓存将仅在该节点上刷新,而不会在其他节点上刷新,并且它们将具有陈旧数据。

My question is, can I somehow fix this using MemoryCache, or I need to do something else, and if I do, what are the possible solutions?我的问题是,我能否使用 MemoryCache 以某种方式解决此问题,或者我需要做其他事情,如果我这样做,可能的解决方案是什么?

I think you are looking for is Distributed Caching我认为您正在寻找的是分布式缓存

Using the IDistributedCache interface you can use either Redis or Sql Server and it supplies basic Get/Set/Remove methods.使用 IDistributedCache 接口,您可以使用 Redis 或 Sql Server,它提供基本的 Get/Set/Remove 方法。 Changes made on one node will be available to other nodes.在一个节点上所做的更改将可用于其他节点。

Using Redis is a great way of sharing Session type data between servers in a load balanced environment, Sql Server does not seem to be a great fit given that you seem to be caching to avoid db calls.使用 Redis 是在负载平衡环境中的服务器之间共享会话类型数据的好方法,鉴于您似乎正在缓存以避免数据库调用,Sql Server 似乎不太适合。

It might also be worth considering if you are actually complicating things by caching in the first place.如果您实际上首先通过缓存使事情复杂化,那么也可能值得考虑。 When you have a single application you see the benefit, as keeping them in application memory saves a request over the network, but when you have a load balanced scenario, you have to compare retrieving those records from a distributed cached vs retrieving them from the database.当你有一个单一的应用程序时,你会看到好处,因为将它们保存在应用程序内存中可以节省网络请求,但是当你有一个负载平衡的场景时,你必须比较从分布式缓存中检索这些记录与从数据库中检索它们.

If the data is just an in memory copy of a relatively small database table, then there is probably not a lot to choose performance wise between the two.如果数据只是一个相对较小的数据库表的内存副本,那么在这两者之间可能没有太多性能选择。 If the data is based on a complicated expensive query then the cache is the way to go.如果数据基于复杂的昂贵查询,那么缓存就是最佳选择。

If you are making hundreds of requests a minute for the data, then any network request may be too much, but you can consider what are the consequences of the data being a little stale?如果你每分钟对数据进行数百次请求,那么任何网络请求都可能过多,但你可以考虑数据有点陈旧的后果是什么? For example, if you update a record, and the new record is not available immediately on every server, does your application break?例如,如果您更新一条记录,而新记录并非立即在每台服务器上可用,您的应用程序会中断吗? Or does the change just occur in a more phased way?还是变化只是以更分阶段的方式发生? In that case you could keep your in process memory cache, just use a shorter Time To Live.在那种情况下,您可以保留进程内内存缓存,只需使用更短的生存时间。

If you really need every change to propagate to every node straight away then you could consider using a library like Cache Manager in conjunction with Redis which can combine an in memory cache and synchronisation with a remote cache.如果您确实需要将每个更改立即传播到每个节点,那么您可以考虑将缓存管理器等库与 Redis 结合使用,它可以结合内存缓存和与远程缓存的同步。

Somewhat dated question, but maybe still useful: I agree with what ste-fu said, well explained.有些过时的问题,但也许仍然有用:我同意 ste-fu 所说的,解释得很好。

I'll only add that, on top of CacheManager, you may want to take a look at FusionCache ⚡ , which I recently released.我只会补充一点,在 CacheManager 之上,您可能想看看我最近发布的FusionCache ⚡

On top of supporting an optional distributed 2nd layer transparently managed for you, it also has some other nice features like an optimization that prevents multiple concurrent factory for the same cache key from being executed (less load on the source database), a fail-safe mechanism and advanced timeouts with background factory completion除了支持为您透明管理的可选分布式第 2 层之外,它还有一些其他不错的功能,例如防止为同一缓存键执行多个并发工厂的优化(减少源数据库上的负载)、 故障安全具有后台工厂完成的机制和高级超时

If you will give it a chance please let me know what you think.如果你愿意给它一个机会,请告诉我你的想法。

/shameless-plug /无耻插头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM