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 40 public class FIFOMaintenancePolicy implements CacheMaintenancePolicy { 41 42 private static final Log log = LogFactory.getLog(FIFOMaintenancePolicy.class); 43 44 protected ManagedCache managedCache = null; 45 46 protected int maxSize; 47 48 public void performMaintenance() { 49 log.debug(Messages.getString("FIFOMaintenancePolicy.performing_fifo_maintenance")); 51 final Object [] args = { 52 new Integer (maxSize), 53 new Integer (managedCache.size()) 54 }; 55 log.debug( Messages.getCompoundString("CacheMaintenancePolicy.report_items", args) ); 57 final List entries = new ArrayList (managedCache.entrySet()); 59 int currentSize = managedCache.size(); 60 if (maxSize < currentSize) { 61 final Object [] args1 = { 62 new Integer (currentSize - maxSize) 63 }; 64 log.debug( Messages.getCompoundString("CacheMaintenancePolicy.clearing_approximately", args1) ); Collections.sort(entries, new AddedComparator()); 66 final List removeThese = entries.subList(0, currentSize - maxSize); 67 for (Iterator i = removeThese.iterator(); i.hasNext();) { 68 final Map.Entry entry = (Entry) i.next(); 69 if (entry != null) { 70 managedCache.remove(entry.getKey()); 72 } 73 } 74 log.debug(Messages.getString("FIFOMaintenancePolicy.new_size") + managedCache.size()); } 76 77 } 78 79 public void setCache(final ManagedCache _cache) { 80 managedCache = _cache; 81 } 82 83 public void setConfiguration(final CacheConfiguration _configuration) { 84 maxSize = _configuration.getMaxSize(); 85 } 86 }
| Popular Tags
|