views:

268

answers:

9

Hi all, sorry for the duplication, I'm really stuck on this. I posted this before and someone did try to help, but I wasn't signed in at the time and I can't seem to find it now...

I've cached a value using the ASP.NET Cache, with the following code:

Cache.Insert("TEST_VALUE", 150, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(120));

As I understand it, this should mean that if nothing accesses that object for 120 seconds, it will expire and return null.

However, if after 10 minutes I run a page which writes out that value from the cache, it's still there (and indeed still after a whole hour). I know there's nothing else accessing it, because this is all on a local server on my machine.

Can anyone tell me why these values aren't getting purged from the cache?

A: 

Dupe of your original question here:

http://stackoverflow.com/questions/337897/whats-the-reason-for-my-aspnet-cache-never-expiring-despite-having-a-sliding-ex

Suggest you supply more info on the context of the caching.

Ben Aston
A: 

Ah ok, sorry - the caching is being done in the HttpApplication context, from within an HttpModule.

Mark B
A: 

Are you absolutely sure you don't just insert the item into the cache on every page hit?

The reason I ask is because you mention that it's done in an HttpModule and they're usually executed on every page hit.

That's the only thing that would explain this behavior as far as I can tell.

I suggest you set a breakpoint on the line where the Cache.Insert happens and see if it executes when you hit the page. You could also subscribe to the remove callback of the Cache and write to a file when it expires. If you do that you can execute the page once; so that the item is inserted and then just sit and watch the file to see if it gets removed.

Markus Olsson
A: 

Thanks, I see what you mean, but my HttpModule is checking the type of request before inserting anything into the cache, so it will only occur on form uploads.

I've tried what you suggested anyway, and the breakpoint never gets hit when I am refreshing the page that displays the cached value.

I am assuming that even reading from the cache should extend the lifespan of the object, so I am leaving it 5-10 minutes before refreshing the 'debugging' page, but the value is still there!

Mark B
+1  A: 

Put a breakpoint on all instances of cache reads to see if they are hit unexpectedly (thus prolonging the life of the object)?

Ben Aston
+3  A: 

Just a debugging tip: Set the value of the cached object to DateTime.Now when you insert it in order to see when it was inserted. That way you can easily determine if it was re-inserted somehow or if something somewhere keeps accessing and thus forcing it to stay in the cache.

That combined with my suggestion about subscribing to the cache expiry event and writing to a log file should provide you with enough information to locate the problem.

Markus Olsson
A: 

OK, I think I've figured it out, but I'm not sure exactly why this is:

I did as you both suggested - there was definitely nothing reading from the cache, so I wrote a callback which wrote the removal time to a log file and subscribed it to the expiry event. I also wrote the time each item was added to the cache into the same log, so I could see how long things were staying in there.

After I'd assigned the value to the Cache initially in the code, there was then a loop where I assigned a new value to the same object on each iteration, using:

Cache["TEST_VALUE"] = counter;

However with this syntax, as soon as this happened the log file showed the item had been removed from the cache, even though it was still accessible by the separate page that just wrote out the values of the cached objects?

I don't understand why this should be the case - I understood that the above syntax would just update the value of the object in the cache, but maybe I have this wrong?

Messing around, I changed the code so that the above line in the loop now read:

Cache.Insert("TEST_VALUE", newvalue, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(120));

And now in the log I get the same removal notification, followed immediately by another addition notification, and the item expires correctly after 120(ish) seconds and is no longer accessible from other pages.

I'm very confused, but thanks for your help :)

Mark B
A: 

Verify

<cache disableExpiration="true"/>

is not set in your web.config?

Ben Aston
A: 

No, that is definitely not set; as I said above, using:

Cache.Insert("TEST_VALUE", newvalue, options);

instead of:

Cache["TEST_VALUE"] = newvalue;

causes the objects to expire correctly.

Mark B