.NET Caching how does Sliding Expiration work?
There are two types of cache policies you can use:
CacheItemPolicy.AbsoluteExpiration
will expire the entry after a set amount of time.
CacheItemPolicy.SlidingExpiration
will expire the entry if it hasn't been accessed in a set amount of time.
The ObjectCache Add()
overload you're using treats it as an absolute expiration, which means it'll expire after 1 day, regardless of how many times you access it. You'll need to use one of the other overloads. Here's how you'd set a sliding expiration (it's a bit more complicated):
CacheItem item = cache.GetCacheItem("item");
if (item == null) {
CacheItemPolicy policy = new CacheItemPolicy {
SlidingExpiration = TimeSpan.FromDays(1)
}
item = new CacheItem("item", someData);
cache.Set(item, policy);
}
You change the TimeSpan to the appropriate cache time that you want.