protected void computeMeasured(K key, Computable<? super K, ? extends V> c, Holder<K, V> ft) throws Exception { long latency = System.nanoTime(); try { ft.compute(c, key); } finally { latency = System.nanoTime() - latency; statistics.computeCount.incrementAndGet(); statistics.computeLatency.addAndGet(latency); } }
protected V getFromHolder(Holder<K, V> holder) { return holder == null ? null : holder.get(); }
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)); }
Holder<K, V> ft = new Holder<K, V>(key, timingsHolder); f = map.putIfAbsent(key, ft); if (f == null) { throw Unchecked.rethrow(e); } finally { ft.done(); if (failed && removeOnException) { map.remove(key); f.setRemoved(); } else { updateTimingCache(f); onAddItem(f.getKey(), f.get()); if (outdated != null) outdated.remove(key);
public void removeOldest() { Holder<K, V> holder = null; TimingsHolder<K, V> th; Iterator<WeakReference<TimingsHolder<K, V>>> i = timings.values().iterator(); while ((th = next(i)) != null) { Iterator<TimingEntry<Holder<K, V>>> iterator = th.timings.iterator(); while (iterator.hasNext()) { TimingEntry<Holder<K, V>> next = iterator.next(); Holder<K, V> temp = next.value.get(); if (temp == null || temp.isRemoved() || next.timing != temp.validUntil) { iterator.remove(); continue; } if (holder == null || temp.validUntil < holder.validUntil) holder = temp; break; } } if (holder != null) remove(holder.getKey()); }
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 long getTTL(K k) { Holder<K, V> holder = map.get(k); if (holder != null) return holder.getTimingsHolder().ttl; return ttl; }
protected void onRemoveItem(Holder<K, V> h, boolean putToOutdated) { if (putToOutdated) putToOutdated(h); h.setRemoved(); onRemoveItem(h.k, h.v); }
if (h == null || h.isRemoved() || h.validUntil != entry.timing) { timings.poll(); } else if (entry.timing <= time) {
Holder<K, V> ft = new Holder<K, V>(key, timingsHolder); f = map.putIfAbsent(key, ft); if (f == null) { throw Unchecked.rethrow(e); } finally { ft.done(); if (failed && removeOnException) { map.remove(key); f.setRemoved(); } else { updateTimingCache(f); onAddItem(f.getKey(), f.get()); if (outdated != null) outdated.remove(key);
public void removeOldest() { Holder<K, V> holder = null; TimingsHolder<K, V> th; Iterator<WeakReference<TimingsHolder<K, V>>> i = timings.values().iterator(); while ((th = next(i)) != null) { Iterator<TimingEntry<Holder<K, V>>> iterator = th.timings.iterator(); while (iterator.hasNext()) { TimingEntry<Holder<K, V>> next = iterator.next(); Holder<K, V> temp = next.value.get(); if (temp == null || temp.isRemoved() || next.timing != temp.validUntil) { iterator.remove(); continue; } if (holder == null || temp.validUntil < holder.validUntil) holder = temp; break; } } if (holder != null) remove(holder.getKey()); }
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 long getTTL(K k) { Holder<K, V> holder = map.get(k); if (holder != null) return holder.getTimingsHolder().ttl; return ttl; }
protected void onRemoveItem(Holder<K, V> h, boolean putToOutdated) { if (putToOutdated) putToOutdated(h); h.setRemoved(); onRemoveItem(h.k, h.v); }
if (h == null || h.isRemoved() || h.validUntil != entry.timing) { timings.poll(); } else if (entry.timing <= time) {
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)); }
protected V getFromHolder(Holder<K, V> holder) { return holder == null ? null : holder.get(); }
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(); } }
protected void computeMeasured(K key, Computable<? super K, ? extends V> c, Holder<K, V> ft) throws Exception { long latency = System.nanoTime(); try { ft.compute(c, key); } finally { latency = System.nanoTime() - latency; statistics.computeCount.incrementAndGet(); statistics.computeLatency.addAndGet(latency); } }
public V remove(K k) { Holder<K, V> holder = map.remove(k); if (holder == null) return null; long latency = System.nanoTime(); try { onRemoveItem(holder, true); return holder.get(); } finally { latency = System.nanoTime() - latency; statistics.removeCount.incrementAndGet(); statistics.removeLatency.addAndGet(latency); updateSizeMetric(); } }