1 3 package jodd.cache; 4 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Timer ; 8 import java.util.TimerTask ; 9 10 15 public class TimedCache extends AbstractCacheMap { 16 17 public TimedCache(long timeout) { 18 this.cacheSize = 0; 19 this.timeout = timeout; 20 cacheMap = new HashMap (); 21 } 22 23 25 28 public synchronized int prune() { 29 int count = 0; 30 Iterator values = cacheMap.values().iterator(); 31 while (values.hasNext()) { 32 CacheObject co = (CacheObject) values.next(); 33 if (co.isExpired() == true) { 34 values.remove(); 35 count++; 36 } 37 } 38 return count; 39 } 40 41 42 44 protected Timer pruneTimer; 45 46 49 public void schedulePrune(long delay) { 50 if (pruneTimer != null) { 51 pruneTimer.cancel(); 52 } 53 pruneTimer = new Timer (); 54 pruneTimer.schedule( 55 new TimerTask () { 56 public void run() { 57 prune(); 58 } 59 }, delay, delay 60 ); 61 } 62 63 66 public void cancelPruneSchedule() { 67 if (pruneTimer != null) { 68 pruneTimer.cancel(); 69 pruneTimer = null; 70 } 71 } 72 73 } 74 | Popular Tags |