1 17 18 package com.whirlycott.cache.policy; 19 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Map.Entry; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.whirlycott.cache.CacheConfiguration; 31 import com.whirlycott.cache.CacheMaintenancePolicy; 32 import com.whirlycott.cache.ManagedCache; 33 import com.whirlycott.cache.Messages; 34 35 41 public class LRUMaintenancePolicy implements CacheMaintenancePolicy { 42 43 private static final Log log = LogFactory.getLog( LRUMaintenancePolicy.class ); 44 45 protected ManagedCache managedCache = null; 46 47 protected int maxSize; 48 49 public void performMaintenance() { 50 log.debug( Messages.getString("LRUMaintenancePolicy.performing_lru_maintenance") ); 52 final Object [] args = { 53 new Integer (maxSize), 54 new Integer (managedCache.size()) 55 }; 56 log.debug( Messages.getCompoundString("CacheMaintenancePolicy.report_items", args) ); 58 final List entries = new ArrayList ( managedCache.entrySet() ); 60 int currentSize = managedCache.size(); 61 if ( maxSize < currentSize ) { 62 final Object [] args1 = { 63 new Integer (currentSize - maxSize) 64 }; 65 log.debug( Messages.getCompoundString("CacheMaintenancePolicy.clearing_approximately", args1) ); Collections.sort( entries, new UsedComparator() ); 67 final List removeThese = entries.subList( 0, currentSize - maxSize ); 68 for (final Iterator i = removeThese.iterator(); i.hasNext();) { 69 final Map.Entry entry = (Entry) i.next(); 70 if (entry != null) { 71 managedCache.remove( entry.getKey() ); 76 } 77 } 78 log.debug( Messages.getString("LRUMaintenancePolicy.new_size") + managedCache.size() ); } 80 81 } 82 83 public void setCache(ManagedCache _cache) { 84 managedCache = _cache; 85 } 86 87 public void setConfiguration(CacheConfiguration _configuration) { 88 maxSize = _configuration.getMaxSize(); 89 } 90 }
| Popular Tags
|