asp.net缓存和回收工作进程 [英] asp.net cache and recycle worker processes

查看:108
本文介绍了asp.net缓存和回收工作进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我注意到,只要回收工作进程,就会清除HttpContext.Cache.我有一个回调,可以在过期时将缓存的项目重新插入到缓存中.但是,在回收过程中似乎不会发生这种情况.

So I've noticed that HttpContext.Cache is cleared whenever the worker process is recycled. 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.

我在我的Application_Start中有一个调用,该调用将适当的项添加到缓存中,但是在回收工作进程之后,似乎没有调用该调用.是否有回调可用于在回收时重新填充缓存,还是应该为Application_Start?另外,如何在本地进行测试?

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. Is there a callback I can use to repopulate the cache on recycling or is it supposed to be Application_Start? Also, how can I test this locally?

我可以肯定我在这里做错了.

I'm fairly certain I'm doing something wrong here.

推荐答案

工作循环的过程可以看做是停止并启动拥有.Net的进程.所有AppDomain数据都会丢失.

Worked process recycle can be looked as stopping and starting the process holding .Net. All AppDomain data is lost.

据我了解,只有在第一个请求到达时才执行Application_Start.我希望回收后的行为也一样.

From my understanding Application_Start is only executed when first request arrives. I'd expect the behaviour to be the same even after a recycle.

但是,如果它不执行(有趣),那么您可以随时在其他地方触发它.有很多方法可以做到这一点.例如Application_BeginRequest或首次加载时引用的任何静态类的ctor.

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.

请注意,HttpContext.Cache实际上只是一个静态对象,因此您只需拥有一个static bool isInitialized = false;,就可以在完成初始化后对其进行更改并将其保存在请求中.放置在适当位置的lock() {}可以顺利运行(因此,两个请求不会启动两个初始化).

Note that HttpContext.Cache is actually just a static object, so you can simply have a static bool isInitialized = false; that you change once init is done and it will be kept across requests. With a well-placed lock() {} it should run smoothly (so two requests don't start two initializations).

我通常解决的方法是按需缓存.不适用于所有解决方案.另外,我使用Enterprise Framework或AppFabric为缓存设置超时(TTL).

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.

这篇关于asp.net缓存和回收工作进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆