Skip to content

ConcurrentLru: Time‐based eviction

Alex Peck edited this page Nov 16, 2023 · 11 revisions

ConcurrentLru supports 3 modes of time based eviction:

  • Expire after access: sliding window
ICache<string, int> expireAfter = new ConcurrentLruBuilder<string, int>()
   .WithExpireAfterAccess(TimeSpan.FromSeconds(10))
   .Build();
  • Expire after write: absolute
ICache<string, int> expireAfter = new ConcurrentLruBuilder<string, int>()
   .WithExpireAfterWrite(TimeSpan.FromSeconds(10))
   .Build();
  • Expire after: fully configurable
public class Expiry : IExpiryCalculator<string, int>
{
    public Duration GetExpireAfterCreate(string key, int value) => Duration.FromSeconds(10);
    public Duration GetExpireAfterRead(string key, int value, Duration current) => current;
    public Duration GetExpireAfterUpdate(string key, int value, Duration current) => current;
}

ICache<string, int> expireAfter = new ConcurrentLruBuilder<string, int>()
   .WithExpireAfter(new Expiry())
   .Build();

Expiration is performed as part of the queue cycling logic whenever a new value is added to the cache. If the turnover of items is low, or there is little cache activity items will remain in the cache.

Expired items can also be removed by calling the TrimExpired method of the appropriate cache policy. This can be done in the background if eager expiry is required. Trim and TrimExpired are both O(n) operations, where n is the number of items in the cache.

Clone this wiki locally