System.Runtime.Cache not expiring?
I have implemented a solution in my ASP.NET project to automatically send some emails based on a time. I have done this by using the System.Runtime.Cache, specifically the CacheItemRemovedCallback. First开发者_JS百科 of all i add the task to the cache in the Application_Start method:
protected void Application_Start(object sender, EventArgs e)
{
...
AddTask(reportElement.name, totalMinutes);
...
}
and the AddTask method then adds the item to the cache:
private void AddTask(string name, int minutes)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, minutes, null, DateTime.Now.AddMinutes(minutes), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
So when the cache entry expires after the minutes specified in the AbsolutionExpiration, it calls my CacheItemRemoved method. This basically runs a report, sends an email and then re-adds the task to the cache, so it will run again after the time has expired again - simple. Here is part of the code we are concerned with in the CacheItemRemoved.
public void CacheItemRemoved(string taskName, object minutes, CacheItemRemovedReason r)
{
...
finally
{
AddTask(taskName, Convert.ToInt32(minutes));
}
...
}
There is exception handling in the code, as you can see the re-adding of the task is in the finally block, so should always get called. And all the exception catch blocks do is log the error to the file, as i want to keep the task running even if the previous one fails.
This works perfectly on my local machine, but when on a Windows Server 2003, it basically just runs once. I have added extra debugging and it looks like the second time the cache entry is added, it simply doesn't expire. I am completely stuck now. The windows server is running IIS 6.0. Are there any settings for the cache i don't know about. Also, on the server it seems to expire at a completely different time to what was specified in the minutes.
Thanks in advance.
HttpRuntime.Cache.Insert(name, minutes, null, DateTime.Now.AddMinutes(minutes), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
When you add your cache item why are you specifying CacheItemPriority.NotRemovable, surely this will prevent the item from ever being removed (unless you run out of memory).
精彩评论