public Cache(String name, long ttlSec, Computable<? super K, ? extends V> computable) { this.name = name != null ? name : "Cache-" + NAME_COUNTER.incrementAndGet(); this.ttl = ttlSec * 1000; this.computable = computable; statistics = createStatistics(); timings.put(ttl, new WeakReference<TimingsHolder<K, V>>(timingsHolder = new TimingsHolder<K, V>(ttl))); CacheCleaner.addCache(this); }
private void updateTimingCache(final Holder<K, V> key) { TimingsHolder<K, V> timingsHolder = key.getTimingsHolder(); if (timingsHolder == null || timingsHolder.ttl <= 0) return; long timing = timingsHolder.ttl + System.currentTimeMillis(); key.setValidUntil(timing); CacheCleaner.updateWakeUp(timing); timingsHolder.timings.add(new TimingEntry<Holder<K, V>>(key, timing)); }
@Test public void test_cache_iterable() { Cache<String, String> cache; cache = new Cache<String, String>(1); cache = new Cache<String, String>(1); cache = new Cache<String, String>(1); int before = CacheCleaner.size(); Assert.assertTrue(before >= 3); System.gc(); int i = 0; for (Cache c : CacheCleaner.iterable()) { i++; } Assert.assertEquals(1, i); Assert.assertEquals(1, CacheCleaner.size()); }
@Test public void destroy_test() throws InterruptedException { System.gc(); Cache<String, String> cache = new Cache<String, String>(1, new Computable<String, String>() { @Override public String compute(String s) { return s.toUpperCase(); } }); cache.get("foo"); Assert.assertEquals(1, cache.size()); Thread.sleep(1020); Assert.assertEquals(0, cache.size()); Assert.assertEquals(1, CacheCleaner.size()); cache.get("foo"); Assert.assertEquals(1, cache.size()); cache.destroy(); Assert.assertEquals(0, cache.size()); Assert.assertEquals(true, cache.isDestroyed()); Assert.assertEquals(1, CacheCleaner.size()); Thread.sleep(1020); Assert.assertEquals(0, CacheCleaner.size()); }
public Cache(String name, long ttlSec, Computable<? super K, ? extends V> computable) { this.name = name != null ? name : "Cache-" + NAME_COUNTER.incrementAndGet(); this.ttl = ttlSec * 1000; this.computable = computable; statistics = createStatistics(); timings.put(ttl, new WeakReference<TimingsHolder<K, V>>(timingsHolder = new TimingsHolder<K, V>(ttl))); CacheCleaner.addCache(this); }
private void updateTimingCache(final Holder<K, V> key) { TimingsHolder<K, V> timingsHolder = key.getTimingsHolder(); if (timingsHolder == null || timingsHolder.ttl <= 0) return; long timing = timingsHolder.ttl + System.currentTimeMillis(); key.setValidUntil(timing); CacheCleaner.updateWakeUp(timing); timingsHolder.timings.add(new TimingEntry<Holder<K, V>>(key, timing)); }