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?
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!
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:
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:
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 :)
As Long as your computer is on and the memory block is not replace by some other data. The Cache will never expire. Even if you Close your browser and re open it, you can retrieve it.
e.g.
cache("Key") = 10
textbox.text = cache("Key")
.That is a correct assumption on your part. Where are you doing the caching?
The reason that I ask is that if you are doing it in the Global.asax, in Application_Start, for example, and the application is being recycled between page views, it would exhibit this same behavior.
Put a breakpoint on all instances of cache reads to see if they are hit unexpectedly (thus prolonging the life of the object)?
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.
Dupe of your original question here:
What's the reason for my ASP.NET Cache never expiring, despite having a sliding expiration time set?
Suggest you supply more info on the context of the caching.