Cache Item Purging

Topics: Caching Application Block
Jun 13, 2011 at 10:03 PM

Is there a way to remove the least frequently used  or least recently used item from entlib caching block ?

Jun 14, 2011 at 2:28 AM
Edited Jun 14, 2011 at 2:30 AM


You can use the Sliding Time Expiration Policy to achieve this. For example you would want to remove an item which has not been accessed for the last 1 minute. In your code:

            ICacheItemExpiration expire = new SlidingTime(TimeSpan.FromMinutes(1))
            ICacheManager manager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); 
            manager.Add("MyKey", "MyValue", CacheItemPriority.Normal, null, expire );

The timer will be reset after the item has been accessed.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Jun 14, 2011 at 2:49 PM


In situations where the available memory is running low, I was hoping Entlib Caching has a scavenging plan which automatically removes least frequently used items in order to make space for new items.

assuming available memory is running low and I try to add a new Item to Cache, does Entlib Caching have any in-built plan for purging.

Jun 15, 2011 at 3:15 AM
Edited Jun 15, 2011 at 6:39 AM


I can't find a good documentation for this one. According to the Design of the Scavenging Process:

"The BackgroundScheduler object determines which items should be scavenged by doing a major sort based on priority and a minor sort based on the last time the item was accessed"

I'm quite sure scavenging happens when you exceeded the value defined in maximumElementsInCacheBeforeScavenging propery of cache manager. But I'm not sure about scavenging will take place on low memory scenario, though according to Exploring Caching : Using Caching Applciation Enterprise Library 4.1 in CodeProject:

"Scavenging means that the cache attempt to remove infrequently used or unimportant items if memory becomes low..."
Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Jun 15, 2011 at 7:28 PM

Thanks a million. 

I believe we can use the maximumelementsincachebeforescavenging property to achieve purging.

Thanks again