简体   繁体   English

ASP.NET InProc缓存与分布式缓存

[英]ASP.NET InProc caching vs distributed cache

I'm looking into Redis and alternatives because we are going to switch to writing our applications distributed. 我正在研究Redis和替代方案,因为我们将切换到编写分布式应用程序。 My thought was that we need distributed caching such as Redis to ensure that we have a consistent cache everywhere. 我的想法是,我们需要像Redis这样的分布式缓存,以确保我们到处都有一致的缓存。 My senior colleague does not agree and says that we should just use selective InProc caching, where some data is cached in the machine's memory when it's requested. 我的高级同事不同意,他说我们应该只使用选择性的InProc缓存,在这种情况下,一些数据会在被请求时缓存在机器的内存中。 He also said that Redis will be much slower than caching the data InProc. 他还说,Redis比缓存InProc数据要慢得多。 He agrees that we should store Session state in a distributed cache because that needs to be consistent. 他同意我们应该将会话状态存储在分布式缓存中,因为这需要保持一致。

What is the best place to keep a cache? 保留缓存的最佳位置是什么? InProc or distributed? 是InProc还是分布式?

Using in-proc or out-proc cache is purely application dependent. 使用进程内或进程外缓存完全取决于应用程序。

Inproc cache stores data in current application's process memory which makes cached data access very fast however cached data is accessible only to the local application. Inproc缓存将数据存储在当前应用程序的进程内存中,这使得对缓存的数据的访问非常快,但是缓存的数据只能由本地应用程序访问。 This works fine if you have only one application server or if every application server is using a different data set. 如果您只有一个应用程序服务器,或者每个应用程序服务器都使用不同的数据集,则此方法可以正常工作。 Even so, if the application server goes down, the cached data will be lost. 即使这样,如果应用程序服务器出现故障,缓存的数据也会丢失。

However if your multiple application server using same set of data, Inproc cache is not the best solution. 但是,如果您的多个应用程序服务器使用同一组数据,则Inproc缓存不是最佳解决方案。 Since, in that case, every application would be loading the same data set hence limiting the usefulness of using cache. 因为在那种情况下,每个应用程序都将加载相同的数据集,因此限制了使用缓存的有用性。 Moreover, for session state caching, it will leave you with the only option of using sticky-sessions which, in turn, would limit load balancing. 此外,对于会话状态缓存,它将为您提供使用粘性会话的唯一选择,从而限制了负载平衡。

On the other hand, distributed caching will add an extra network cost of getting data from another server, but it would give you an advantage of sharing the same data set with all other applications. 另一方面,分布式缓存将增加从另一台服务器获取数据的网络成本,但它将为您提供与所有其他应用程序共享相同数据集的优势。 Not only that but also, the data would remain cached even if the application server goes down. 不仅如此,即使应用程序服务器出现故障,数据也将保持高速缓存。

You can also use a hybrid solution of both Inproc and OutProc caching, like one provided by NCache , where you can have a distributed clustered cache (containing all cached data) and a local inproc cache (containing subset of data, frequently used by that application server). 您还可以使用Inproc和OutProc缓存的混合解决方案,例如NCache提供的一种,您可以在其中拥有分布式集群缓存(包含所有缓存的数据)和本地inproc缓存(包含数据的子集,该应用程序经常使用)服务器)。 This will give you advantages of both caching techniques. 这将为您提供两种缓存技术的优势。

Since you are re-writing your application, I will recommend you to try NCache . 由于您正在重新编写应用程序,因此我建议您尝试使用NCache It provides both in-proc and out-proc solutions. 它提供过程中和过程外解决方案。 You can write your application only once, test it with both solutions and go with the one which suits you best. 您只能编写一次应用程序,使用这两种解决方案进行测试,然后选择最适合您的解决方案。

InProc and Distributed caching aren't mutually exclusive, or your only options. InProc和分布式缓存不是互斥的,也不是您唯一的选择。 You can have distributed invalidation solutions as well. 您也可以具有分布式失效解决方案

You should also more closely examine what you really need with respect to a "consistent" cache. 您还应该更仔细地检查关于“一致”缓存的真正需求。 The only way to guarantee that session state be truly consistent is to lock it. 确保会话状态真正一致的唯一方法是锁定它。 This will serialize simultaneous requests from the same user. 这将序列化来自同一用户的同时请求。

Are you sure you really want/need session state at all? 您确定您确实真的想要/需要会话状态吗? If the things you want to track are security based, are you sure you cannot deal with slightly stale data? 如果您要跟踪的内容基于安全性,您确定不能处理稍微陈旧的数据吗? If it's user state based, it should likely be rendered into the client page, not independently tracked on the server. 如果基于用户状态,则应将其呈现到客户端页面中,而不是在服务器上独立跟踪。

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

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