Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the caching 'Priority' and 'AbsoluteExpiration' work together?

Tags:

c#

caching

I found myself into a CacheItem that didn't clean up correctly. While looking at MSDN and correct myself into using Utc-based calculation, I found this confusing information:

  • CacheItemPolicy.Priority
  • CacheItemPolicy.AbsoluteExpiration

AbsolutExpiration is used to set a "keep-alive" of a CacheItem, Priority.NotRemovable is used to force CacheItem to exist forever. No notification about what property overrides the other.

The code below do compile and SQL Profiler also confirm that the database is queried only once, while every other request came from cache.

CacheItemPolicy _cachePolicy = new CacheItemPolicy() 
{
    AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(6)),
    Priority = CacheItemPriority.NotRemovable 
};

I assume that this code force the cache items to stay forever but are cleared after 12 hours from creation, in line with the MSDN's note about the setting.

"Cache implementations should set the NotRemovable priority for a cache entry only if the cache implementation provides ways to evict entries from the cache and to manage the number of cache entries"

Then the other side, why would both properties work together at all? Does the implementation bring some kind of "more non-removable"?

like image 456
Independent Avatar asked Oct 28 '11 07:10

Independent


People also ask

What is Absoluteexpiration?

In the absolute expiration you can see that it will expires after one minute whether its accessed or not. While in sliding expiration it will expire cache if cache is not accessed within specified time like one minute.

How to implement caching in ASP.NET Core?

ASP.NET Core supports several different caches. The simplest cache is based on the IMemoryCache. IMemoryCache represents a cache stored in the memory of the web server. Apps running on a server farm (multiple servers) should ensure sessions are sticky when using the in-memory cache.

Where cache data stored in ASP net?

Cache is stored in web server memory.

How does memory cache work C#?

In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.


1 Answers

So according to this "NotRemovable" prevents the cache entry from being removed automatically (like when the cache is running out of space) but will be removed when it expires or you manually take it out of the cache.

NotRemovable The cache items with this priority level will not be automatically deleted from the cache as the server frees system memory. However, items with this priority level are removed along with other items according to the item's absolute or sliding expiration time.

like image 198
Fabian Nicollier Avatar answered Nov 15 '22 12:11

Fabian Nicollier