[英]Why should I only create MemoryCache early in the application lifecycle?
As per MSDN documentation here : 根据此处的 MSDN文档:
Do not create MemoryCache instances unless it is required. 除非需要,否则不要创建MemoryCache实例。 If you create cache instances in client and Web applications, the MemoryCache instances should be created early in the application life cycle. 如果在客户端和Web应用程序中创建缓存实例,则应在应用程序生命周期的早期创建MemoryCache实例。 You must create only the number of cache instances that will be used in your application, and store references to the cache instances in variables that can be accessed globally. 您必须仅创建将在应用程序中使用的缓存实例数量,并将对缓存实例的引用存储在可以全局访问的变量中。 For example, in ASP.NET applications, you can store the references in application state. 例如,在ASP.NET应用程序中,您可以将引用存储在应用程序状态中。 If you create only a single cache instance in your application, use the default cache and get a reference to it from the Default property when you need to access the cache. 如果仅在应用程序中创建单个缓存实例,请使用默认缓存,并在需要访问缓存时从Default属性获取对其的引用。
It doesn't quite explain the reasoning behind this and several other questions have asked about clearing a MemoryCache
, but seems to have not reached a solid conclusion. 它并不能完全解释其背后的原因,还有其他几个有关清除MemoryCache
,但似乎尚未得出可靠的结论。 As I have gathered, my options are either to dispose the MemoryCache
and create a new instance, which is not advised according to the above documentation, or use the enumerator, which is also not advised due to performance issues. 正如我所收集的,我的选择是处置MemoryCache
并创建一个新实例(根据上述文档不建议这样做),或使用枚举器(由于性能问题也不建议使用此枚举器)。 Trim()
has been shown to be unreliable, so I'm investigating why instantiating a MemoryCache
in the middle of my application lifecycle is considered a bad idea. Trim()
被证明是不可靠的,因此我正在研究为什么在应用程序生命周期的中间实例化MemoryCache
被认为是一个坏主意。
To dive into a bit of why I'm curious, the main issue at hand here is that I want my MemoryCache
to work only for the scope of a series of method calls. 为了深入了解我为什么感到好奇,这里MemoryCache
的主要问题是我希望MemoryCache
仅在一系列方法调用的范围内起作用。 These method calls are made across different instances of the same object, which share data retrieved through the database that, although very unlikely, may change in the short span of a few seconds that I've assigned to the expiration window. 这些方法调用是在同一对象的不同实例之间进行的,它们共享通过数据库检索的数据,尽管这种可能性很小,但可能会在我分配给到期窗口的几秒钟内发生变化。 My idea was to create a new MemoryCache
instance whenever I start off the series of method calls to ensure that I have the most recent data while not hitting the database a high number of times for each of those instances. 我的想法是,每当我开始一系列方法调用时,就创建一个新的MemoryCache
实例,以确保我拥有最新的数据,而不会为每个这些实例频繁访问数据库。 However, MSDN advises against creating new instances as well and suggests to have all instances in global variables. 但是,MSDN建议不要创建新实例,建议将所有实例都放在全局变量中。
So in short, why is it recommended to instantiate a MemoryCache
during the beginning of the application lifecycle and why should I have a global reference to MemoryCache
s that I use? 简而言之,为什么建议在应用程序生命周期开始时实例化MemoryCache
?为什么要对我使用的MemoryCache
进行全局引用?
Because it wants contiguous memory. 因为它想要连续的内存。 You want to do it before memory get fragmented. 您想要在内存碎片化之前执行此操作。
I don't know about the global. 我不了解全球。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.