简体   繁体   English

为什么我应该只在应用程序生命周期的早期创建MemoryCache?

[英]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.

相关问题 我应该为每个Paint请求创建新的Pens / Brushes还是在整个应用程序生命周期中保留它们? - Should I create new Pens/Brushes per Paint request or keep them throughout the application lifecycle? 我应该在班级的哪个位置填充MemoryCache? - Where in my class should i populate MemoryCache? 如何在不实例化不必要的缓存对象的情况下填充MemoryCache? - How should I populate a MemoryCache without instantiating the object to be cached unneccesarily? 我应该为其创建控制台应用程序还是公共库? - Should I create a console application or a common library for it? 访问MemoryCache是​​否创建副本? - Does accessing MemoryCache create a copy? 为什么MemoryCache会抛出NullReferenceException - Why does MemoryCache throw NullReferenceException 为什么当我达到大小限制时,我需要在我的大小受限的 MemoryCache 上调用两次 Set? - Why do I need to call twice the Set on my size limited MemoryCache when I hit the size limit? 我应该使用结构映射来处理我的 linq 数据上下文的生命周期吗? - Should i use structuremap to handle lifecycle of my linq datacontext? 我应该仅为测试目的创建新的构造函数吗? - Should I create new constructor only for test purposes? 在.NET中,为什么ObjectCache比MemoryCache是​​首选类型? - In .NET, why is ObjectCache the preferred type over MemoryCache?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM