@Test public void testCaching() throws InterruptedException { int timeout = 50; int timeToSleep = 60; cache = new TimedCache<TimedCacheTest.CacheEntry>(100, timeout); CacheEntry e1a = new CacheEntry(1); CacheEntry e1b = new CacheEntry(1); CacheEntry e1c = new CacheEntry(1); CacheEntry e2 = new CacheEntry(2); assertEquals(false, cache.update(e1a)); assertEquals(true, cache.update(e1a)); assertEquals(true, cache.update(e1b)); assertEquals(true, cache.update(e1c)); assertEquals(false, cache.update(e2)); assertEquals(true, cache.update(e2)); Thread.sleep(timeToSleep); assertEquals(false, cache.update(e1a)); assertEquals(false, cache.update(e2)); }
public boolean update(K key) { Long curr = new Long(System.currentTimeMillis()); Long prev = cache.putIfAbsent(key, curr); if (prev == null) { return false; } if (curr - prev > this.timeoutInterval) { if (cache.replace(key, prev, curr)) { return false; } } return true; }
TimedCache { public boolean update(K key) { Long curr = new Long(System.currentTimeMillis()); Long prev = cache.putIfAbsent(key, curr); if (prev == null) { return false; } if (curr - prev > this.timeoutInterval) { if (cache.replace(key, prev, curr)) { return false; } } return true; } }
TimedCache { public boolean update(K key) { Long curr = new Long(System.currentTimeMillis()); Long prev = cache.putIfAbsent(key, curr); if (prev == null) { return false; } if (curr - prev > this.timeoutInterval) { if (cache.replace(key, prev, curr)) { return false; } } return true; } TimedCache(int capacity, int timeToLive); }
TimedCache { public boolean update(K key) { Long curr = new Long(System.currentTimeMillis()); Long prev = cache.putIfAbsent(key, curr); if (prev == null) { return false; } if (curr - prev > this.timeoutInterval) { if (cache.replace(key, prev, curr)) { return false; } } return true; } TimedCache(int capacity, int timeToLive); long getTimeoutInterval(); boolean update(K key); }
TimedCache { public boolean update(K key) { Long curr = new Long(System.currentTimeMillis()); Long prev = cache.putIfAbsent(key, curr); if (prev == null) { return false; } if (curr - prev > this.timeoutInterval) { if (cache.replace(key, prev, curr)) { return false; } } return true; } TimedCache(int capacity, int timeToLive); long getTimeoutInterval(); boolean update(K key);  }