1 3 package jodd.cache; 4 5 import java.util.LinkedHashMap ; 6 import java.util.Map ; 7 import java.util.Iterator ; 8 9 10 24 public class LRUCache extends AbstractCacheMap { 25 26 public LRUCache(int cacheSize) { 27 this(cacheSize, 0); 28 } 29 30 33 public LRUCache(int cacheSize, long timeout) { 34 this.cacheSize = cacheSize; 35 this.timeout = timeout; 36 cacheMap = new LinkedHashMap (cacheSize + 1, 1.0f, true) { 37 protected boolean removeEldestEntry(Map.Entry eldest) { 38 return size() > LRUCache.this.cacheSize; 39 } 40 }; 41 } 42 43 44 46 49 public synchronized int prune() { 50 if (isPruneExpiredActive() == false) { 51 return 0; 52 } 53 int count = 0; 54 Iterator values = cacheMap.values().iterator(); 55 while (values.hasNext()) { 56 CacheObject co = (CacheObject) values.next(); 57 if (co.isExpired() == true) { 58 values.remove(); 59 count++; 60 } 61 } 62 return count; 63 } 64 } 65 | Popular Tags |