public boolean putIfAbsent(final K key, final V value, long ttl) { long latency = System.nanoTime(); Holder<K, V> h = new Holder<K, V>(key, value, findTimingsHolder(ttl)); if (map.putIfAbsent(key, h) == null) { updateTimingCache(h); try { onAddItem(key, value); } finally { latency = System.nanoTime() - latency; statistics.putCount.incrementAndGet(); statistics.putLatency.addAndGet(latency); updateSizeMetric(); } return true; } return false; }
public boolean putIfAbsent(final K key, final V value, long ttl) { long latency = System.nanoTime(); Holder<K, V> h = new Holder<K, V>(key, value, findTimingsHolder(ttl)); if (map.putIfAbsent(key, h) == null) { updateTimingCache(h); try { onAddItem(key, value); } finally { latency = System.nanoTime() - latency; statistics.putCount.incrementAndGet(); statistics.putLatency.addAndGet(latency); updateSizeMetric(); } return true; } return false; }
public void put(final K key, final V value, long ttl) { long latency = System.nanoTime(); try { Holder<K, V> h = new Holder<K, V>(key, value, findTimingsHolder(ttl)); Holder<K, V> old = map.put(key, h); onAddItem(key, value); updateTimingCache(h); if (old != null) { long removeLatency = System.nanoTime(); try { onRemoveItem(old, false); } finally { removeLatency = System.nanoTime() - removeLatency; statistics.removeCount.incrementAndGet(); statistics.removeLatency.addAndGet(removeLatency); } } } finally { latency = System.nanoTime() - latency; statistics.putCount.incrementAndGet(); statistics.putLatency.addAndGet(latency); updateSizeMetric(); } }
public void put(final K key, final V value, long ttl) { long latency = System.nanoTime(); try { Holder<K, V> h = new Holder<K, V>(key, value, findTimingsHolder(ttl)); Holder<K, V> old = map.put(key, h); onAddItem(key, value); updateTimingCache(h); if (old != null) { long removeLatency = System.nanoTime(); try { onRemoveItem(old, false); } finally { removeLatency = System.nanoTime() - removeLatency; statistics.removeCount.incrementAndGet(); statistics.removeLatency.addAndGet(removeLatency); } } } finally { latency = System.nanoTime() - latency; statistics.putCount.incrementAndGet(); statistics.putLatency.addAndGet(latency); updateSizeMetric(); } }
@Test public void test_cache_timings_cleanup() { Cache<String, String> cache = new Cache<String, String>(1); Assert.assertEquals(1, cache.timings.size()); Cache.TimingsHolder<String, String> timingsHolder = cache.findTimingsHolder(2); Assert.assertEquals(2, cache.timings.size()); System.gc(); Assert.assertEquals(2, cache.timings.size()); timingsHolder = null; System.gc(); Assert.assertEquals(2, cache.timings.size()); cache.refresh(); Assert.assertEquals(1, cache.timings.size()); int hash = cache.findTimingsHolder(2).hashCode(); Assert.assertEquals(2, cache.timings.size()); Assert.assertEquals(hash, cache.findTimingsHolder(2).hashCode()); System.gc(); Assert.assertEquals(2, cache.timings.size()); cache.findTimingsHolder(2); Assert.assertNotEquals(hash, cache.findTimingsHolder(2).hashCode()); } }