简体   繁体   English

asp.net缓存和回收工作进程

[英]asp.net cache and recycle worker processes

So I've noticed that HttpContext.Cache is cleared whenever the worker process is recycled. 所以我注意到只要工作进程被回收,就会清除HttpContext.Cache I have a callback that reinserts the cached item back into the cache whenever it expires. 我有一个回调,它会在缓存项到期时将其重新插入缓存。 However, this does not seem to happen when the process is recycled. 但是,当该过程被回收时,似乎不会发生这种情况。

I have a call within my Application_Start that adds the appropriate items to the Cache but this does not seem to be called after the worker process is recycled. 我在Application_Start中有一个调用,它将相应的项添加到Cache中,但是在回收工作进程之后似乎没有调用它。 Is there a callback I can use to repopulate the cache on recycling or is it supposed to be Application_Start ? 是否有回调我可以用来在回收时重新填充缓存,或者它应该是Application_Start Also, how can I test this locally? 另外,我如何在本地测试?

I'm fairly certain I'm doing something wrong here. 我很确定我在这里做错了什么。

Worked process recycle can be looked as stopping and starting the process holding .Net. 工作流程回收可以看作停止并启动持有.Net的流程。 All AppDomain data is lost. 所有AppDomain数据都将丢失。

From my understanding Application_Start is only executed when first request arrives. 根据我的理解,Application_Start仅在第一个请求到达时执行。 I'd expect the behaviour to be the same even after a recycle. 我希望即使在回收后,行为也是一样的。

But if it doesn't execute (interesting) then you can always trigger it somewhere else. 但如果它没有执行(有趣),那么你总是可以在其他地方触发它。 There are loads of ways to do that. 有很多方法可以做到这一点。 For instance Application_BeginRequest or the ctor of any static class referencenced during first load. 例如Application_BeginRequest或在第一次加载期间引用的任何静态类的ctor。

Note that HttpContext.Cache is actually just a static object, so you can simply have a static bool isInitialized = false; 注意,HttpContext.Cache实际上只是一个静态对象,所以你可以简单地得到一个static bool isInitialized = false; that you change once init is done and it will be kept across requests. 一旦init完成就会更改,并且它将保留在请求之间。 With a well-placed lock() {} it should run smoothly (so two requests don't start two initializations). 使用良好的lock() {}它应该运行顺畅(因此两个请求不会启动两次初始化)。

The way I usually solve it is to cache on demand. 我通常解决的方法是按需缓存。 Not suitable for all solutions. 不适合所有解决方案。 Also I use Enterprise Framework or AppFabric to set a timeout (TTL) for the cache. 我还使用Enterprise Framework或AppFabric为缓存设置超时(TTL)。

When the AppDomain is recycled, all assemblies are unloaded, and therefore all data is cleared. 当AppDomain被回收时,所有程序集都被卸载,因此所有数据都被清除。 When the app restarts, it is essentially like starting from scratch, there is no way (as far as I am aware) of repopulating the cache this way. 当应用程序重新启动时,它本质上就像从头开始,没有办法(据我所知)以这种方式重新填充缓存。

This is different from stopping and starting the application, which basically saves the state of the application, and upon restart restores it's previous state (including the cache). 这与停止和启动应用程序不同,后者基本上保存了应用程序的状态,并在重新启动时恢复了之前的状态(包括缓存)。

The only thing I can think of is to disable application recycling, but that's not recommended, as you'll end up with an ever-increasing cache size, which would become a bottleneck. 我唯一能想到的就是禁用应用程序回收,但不建议这样做,因为你最终会遇到不断增加的缓存大小,这将成为一个瓶颈。 You could possibly persist the data to a different caching mechanism, such as a SQL database. 您可以将数据持久保存到不同的缓存机制,例如SQL数据库。

The CacheItemPriority enumeration has a "NotRemovable" option that prevents the item from being removed from the cache during garbage collection. CacheItemPriority枚举具有“NotRemovable”选项,可防止在垃圾回收期间从缓存中删除该项。

From MSDN: 来自MSDN:

The cache items with this priority level will not be automatically deleted from the cache as the server frees system memory. 当服务器释放系统内存时,具有此优先级的缓存项不会自动从缓存中删除。 However, items with this priority level are removed along with other items according to the item's absolute or sliding expiration time. 但是,具有此优先级的项目将根据项目的绝对或滑动到期时间与其他项目一起删除。

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

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