public V get(final Object key) { return getValueSafe(getTimestampedValue(key)); }
public void evictExpiredLocalElements() { if (isEvictionEnabled()) { invalidateCacheEntries(); } }
public V put(final K key, final V value) { // ensure evictor is started once we put the first value startEvictionIfNecessary(); return getValueSafe(this.data.put(key, newEntry(value))); }
private void checkExpired(final Object key, final boolean markUsed) { TimestampedValue<V> entry = this.data.get(key); if (null == entry) return; if (isEvictionEnabled()) { int now = getTime(); if (entry.isExpired(now, config)) { removeNoReturn(key); } else if (markUsed) { entry.markUsed(now, null, config); } } }
public V replace(final K key, final V value) { return getValueSafe(this.data.replace(key, newEntry(value))); }
private void invalidateCacheEntries() { int totalCnt = 0; int evaled = 0; Iterator<Entry<K, TimestampedValue<V>>> entryIter = data.entrySet().iterator(); while (entryIter.hasNext()) { Map.Entry<K, TimestampedValue<V>> entry = entryIter.next(); TimestampedValue tsEntry = entry.getValue(); if (tsEntry == null) continue; totalCnt++; if (tsEntry.isExpired(getTime(), config)) { evaled++; entryIter.remove(); } } if (isStatisticsEnabled()) { statistics.increment(totalCnt, evaled); } }
public <K, V> DistributedCache<K, V> newCache() { final MutableConfig copyConfig = new MutableConfig(this.name, this.loggingEnabled, this.maxTTISeconds, this.maxTTLSeconds, this.orphanEvictionEnabled, this.orphanEvictionPeriod, this.targetMaxInMemoryCount, this.targetMaxTotalCount, this.capacityEvictionPolicyDataFactory); if (Terracotta.isActive()) { return new TerracottaDistributedCache<K, V>(copyConfig); } else { return new LocalCache<K, V>(copyConfig); } }
public boolean containsKey(final Object key) { checkExpired(key, false); return this.data.containsKey(key); }
private TimestampedValue<V> newEntry(final V value) { if (value instanceof TimestampedValue) return (TimestampedValue<V>) value; return new DefaultTimestampedValue<V>(value, getTime()); }
/** * Construct a cache with the given configuration * * @param config cache configuration */ public LocalCache(final CacheConfig config) { this.config = config; this.data = new ConcurrentDistributedMap<K, TimestampedValue<V>>(); initializeOnLoad(); }
/** * Further initialize this object and launch the eviction thread. */ private void initializeOnLoad() { this.timeSource = new SystemTimeSource(); this.evictionScheduler = new EvictionScheduler(config, new Evictor<K>(this)); this.statistics = new EvictionStatistics(); this.data.registerMapSizeListener(new TargetCapacityMapSizeListener(data, getConfig(), null)); }
public TimestampedValue<V> getTimestampedValueQuiet(final Object key) { return getTimestampedValue(key); }
public V remove(final Object key) { return getValueSafe(this.data.remove(key)); }
public V put(final K key, final V value) { // ensure evictor is started once we put the first value startEvictionIfNecessary(); return getValueSafe(this.data.put(key, newEntry(value))); }
private void checkExpired(final Object key, final boolean markUsed) { TimestampedValue<V> entry = this.data.get(key); if (null == entry) return; if (isEvictionEnabled()) { int now = getTime(); if (entry.isExpired(now, config)) { removeNoReturn(key); } else if (markUsed) { entry.markUsed(now, null, config); } } }
public V replace(final K key, final V value) { return getValueSafe(this.data.replace(key, newEntry(value))); }
private void invalidateCacheEntries() { int totalCnt = 0; int evaled = 0; Iterator<Entry<K, TimestampedValue<V>>> entryIter = data.entrySet().iterator(); while (entryIter.hasNext()) { Map.Entry<K, TimestampedValue<V>> entry = entryIter.next(); TimestampedValue tsEntry = entry.getValue(); if (tsEntry == null) continue; totalCnt++; if (tsEntry.isExpired(getTime(), config)) { evaled++; entryIter.remove(); } } if (isStatisticsEnabled()) { statistics.increment(totalCnt, evaled); } }
public <K, V> DistributedCache<K, V> newCache() { final MutableConfig copyConfig = new MutableConfig(this.name, this.loggingEnabled, this.maxTTISeconds, this.maxTTLSeconds, this.orphanEvictionEnabled, this.orphanEvictionPeriod, this.targetMaxInMemoryCount, this.targetMaxTotalCount, this.capacityEvictionPolicyDataFactory); if (Terracotta.isActive()) { return new TerracottaDistributedCache<K, V>(copyConfig); } else { return new LocalCache<K, V>(copyConfig); } }
public TimestampedValue<V> getTimestampedValue(final Object key) { checkExpired(key, true); return this.data.get(key); }
private TimestampedValue<V> newEntry(final V value) { if (value instanceof TimestampedValue) return (TimestampedValue<V>) value; return new DefaultTimestampedValue<V>(value, getTime()); }