Search code examples
asp.net.netcachinglru

Does System.Web.Caching utilize an LRU algorithm?


I was just working on the documentation for an open source project I created awhile back called WebCacheHelper. It's an abstraction on top of the existing Cache functionality in System.Web.Caching.

I'm having trouble finding the details of the algorithm used to purge the cache when the server runs low on memory.

I found this text on MSDN:

When the Web server hosting an ASP.NET application runs low on memory, the Cache object selectively purges items to free system memory. When an item is added to the cache, you can assign it a relative priority compared to the other items stored in the cache. Items to which you assign higher priority values are less likely to be deleted from the cache when the server is processing a large number of requests, whereas items to which you assign lower priority values are more likely to be deleted.

This is still a little vague for my taste. I want to know what other factors are used to determine when to purge a cached object. Is it a combination of the last accessed time and the priority?


Solution

  • Let's take a look at the source code. Purging starts from TrimIfNecessary() method in CacheSingle class. Firstly it tries to remove all expired items in FlushExpiredItems() method of CacheExpires class. If that's not enough it starts iterating through "buckets" in CacheUsage.FlushUnderUsedItems(). Cache usage data/statistics divided into "buckets" according to CacheItemPriority and their statistics/LRU treated separately in each bucket. There're two iteration through buckets. The first iteration removes only newly added items (during last 10 seconds). The second one removes other items. It starts removing items from CacheItemPriority.Low bucket and its LRU items. It stops when removed enough otherwise continues to next LRU items and higher priority buckets. It doesn't touch CacheItemPriority.NotRemovable items as it doesn't add them into usage buckets.