views:

30

answers:

2

I have a ASP.NET application which implements a custom in-memory cache. I'm using this as opposed to ASP.NET's caching mechanism as I needed a more complex way to handle what to drop from the cache. Part of this custom cache is a separate thread which occasionally searches for data to drop from the cache whenever it gets too large.

What I need to do is signal this cache maintenance thread to stop whenever the ASP.NET application 'exits'. I guess this basically amounts to when the web site is stopped in IIS.

Is there a pre-existing event I can utilise to do this? Thanks.

A: 

You can create a Global.asax file and override the Application_End method.

http://www.csharphelp.com/2006/06/setting-up-global-objects-with-the-global-asax-file/

Andy Shellam
I think I read that this is sometimes called whenever the application goes idle - i.e. when it hasn't been used in X minutes. Is this the case? It's not necessarily a deal breaker, but it'd be good to know.Thanks for your reply.
Barguast
That's probably IIS killing off the application pool - they're generally set to be restarted after X number of requests or after Y number of minutes, whichever comes sooner, so you'd need to be mindful of that setting on your server.
Andy Shellam
A: 

From the moment that you have this custom, memory cache, I suppose that your asp.net application communicate with this cache in many ways.

So why not make an a global value on this cashe, and make somthing like that on global.asax

void Application_Start(object sender, EventArgs e) 
{
  GlobalCacheUsed ++;
}

void Application_End(object sender, EventArgs e) 
{
  GlobalCacheUsed --;
}

and your thread just check for that GlobalCacheUsed value

Can I please ask you witch way you use to communicate with your apps ? I have make something similar with you, having class that I use both a database table and other cache that use a memory. My trigger to clear the cache is with user events, or with timers.

Hope this help you.

Aristos
I'm sorry, but I'm not sure what you mean by 'which way you use to communicate with your app'. Try to elaborate a little, and I'll help if I can. :)
Barguast
How you have declare your data, and what is the way that the thread get them, and they are seperate from the pages ? Using static variable ?, using database ?, using something else ?
Aristos