jsr107 / RI

Reference Implementation
Other
96 stars 56 forks source link

Implement RI support for Cache Pruning (removing expired entries) #12

Closed brianoliver closed 11 years ago

brianoliver commented 11 years ago

Now that we have the ability to know when Cache Entries have expired, we need to implement a mechanism to actually remove expired Cache Entries as the current implementation does not actually remove expired entries. Instead said entries just appear and behave as if they are "missing". They are still held onto internally.

This can and should be an asynchronous process.

NOTE: We perhaps should add access and modification counts to each RICachedValue so that the "pruning" process can choose to implement pruning based on LFU (least frequently used), LFA (least frequently accessed) and LFM (least frequently modified), or some other algorithm.

gregrluck commented 11 years ago

The RI is not intended for production use. However it does seem memory-leakish never to remove expired keys even when we know they are expired. There are 7 places in RICache where check for expiry and then fire an expiry listener. I have extracted that out to a processExpiries method which now does the pruning.

private void processExpiries(K key, RICacheEventDispatcher<K, V> dispatcher, V expiredValue) {
    entries.remove(key);
    dispatcher.addEvent(CacheEntryExpiredListener.class, new RICacheEntryEvent<K, V>(this, key, expiredValue, EXPIRED));
  }

Lazy pruning is a good approach and one followed in Ehcache.

We do not have any resource limit on the RI (yet). If we did we would need to implement an eviction policy such as LRU.