简体   繁体   中英

Will ASP.NET application pool recycling refresh a cache on a static object?

I have an application that uses a cache for certain objects, and that cache is implemented as a static Dictionary . I'm looking at using some of this code for a new ASP.NET project. Since the cache will be stored on the server, instead of built every time the application is run, I will need to refresh the cache from time to time.

My question is this: will application pool recycling ever rerun the static constructor of my class which sets up the cache?

I don't know much about how application pools work, but from my understanding, they"shut down" when the site isn't accessed for a long time, and restart when it is accessed again. During this restart, would static objects be "instantiated" again, or are those still preserved in memory through the "shut down" process?

Yes. Static constructors would re-run.

Yet, it's still wiser to initialize data on Application_Start event rather than relying on static constructors because it behaves in more predictable way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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