From: <jbo...@li...> - 2005-09-05 21:45:46
|
Author: adamw Date: 2005-09-05 17:45:40 -0400 (Mon, 05 Sep 2005) New Revision: 1046 Modified: trunk/jira-extensions/cache/src/java/pl/net/mamut/jira/Cache.java Log: Cache with timeouts Modified: trunk/jira-extensions/cache/src/java/pl/net/mamut/jira/Cache.java =================================================================== --- trunk/jira-extensions/cache/src/java/pl/net/mamut/jira/Cache.java 2005-09-05 16:33:01 UTC (rev 1045) +++ trunk/jira-extensions/cache/src/java/pl/net/mamut/jira/Cache.java 2005-09-05 21:45:40 UTC (rev 1046) @@ -1,10 +1,31 @@ package pl.net.mamut.jira; import java.lang.ref.SoftReference; +import java.util.Calendar; import java.util.Collections; import java.util.HashMap; import java.util.Map; +class ObjectWithTime { + private Object o; + private long time; + + private final static int TIMEOUT = 24*60*60*1000; + + public ObjectWithTime(Object o) { + this.o = o; + time = Calendar.getInstance().getTimeInMillis(); + } + + public Object get() { + long now = Calendar.getInstance().getTimeInMillis(); + if (now - time > TIMEOUT) + return null; + + return o; + } +} + /** * @author adamw * A simple SoftReference-based cache. @@ -25,8 +46,13 @@ Object o = cache.get(key); if (o == null) return o; - else - return ((SoftReference) o).get(); + else { + o = ((SoftReference) o).get(); + if (o == null) + return o; + + return ((ObjectWithTime) o).get(); + } } /** @@ -35,7 +61,7 @@ * @param o Value to put. */ public static void put(String key, Object o) { - cache.put(key, new SoftReference(o)); + cache.put(key, new SoftReference(new ObjectWithTime(o))); } /** |