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, "", config); } } }
public V putIfAbsent(final K key, final V value) { return getValueSafe(data.putIfAbsent(key, newEntry(value))); }
private void invalidateCacheEntries() { int totalCnt = 0; int evaled = 0; int notEvaled = 0; int errors = 0; Iterator<Entry<K, TimestampedValue<V>>> entryIter = data.entrySet().iterator(); while (entryIter.hasNext()) { Map.Entry<K, TimestampedValue<V>> entry = entryIter.next(); try { TimestampedValue tsEntry = entry.getValue(); if (tsEntry == null) continue; totalCnt++; if (tsEntry.isExpired(getTime(), config)) { evaled++; entryIter.remove(); } else { notEvaled++; } } catch (Throwable t) { errors++; } } if (isStatisticsEnabled()) { statistics.increment(totalCnt, evaled); } }
public <K, V> DistributedCache<K, V> newCache() { MutableConfig copyConfig = new MutableConfig(name, loggingEnabled, maxTTISeconds, maxTTLSeconds, orphanEvictionEnabled, orphanEvictionPeriod, targetMaxInMemoryCount, targetMaxTotalCount, capacityEvictionPolicyDataFactory); if (CacheConfigFactory.DSO_ACTIVE) { return new DistributedCacheImpl<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()); }
public TimestampedValue<V> getTimestampedValueQuiet(final Object key) { return getTimestampedValue(key); }
/** * Construct with the map configuration * * @param config The configuration */ public LocalCache(final CacheConfig config) { this.config = config; this.data = new ConcurrentDistributedMap<K, TimestampedValue<V>>(); initializeOnLoad(); }
public V remove(final Object key) { return getValueSafe(this.data.remove(key)); }
/** * Further initialize this object and launch the eviction thread. */ public 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())); }
public V replace(final K key, final V value) { return getValueSafe(this.data.replace(key, newEntry(value))); }
/** * Construct a new cache instance based on the configuration changes specified in this instance of the builder so far. * If Terracotta is not being used, a non-clustered implementation will be returned instead. * * @param <K> Key type * @param <V> Value type * @return Map constructed by this method based on previous parameters and defaults */ public <K, V> DistributedCache<K, V> newCache() { MutableConfig copyConfig = new MutableConfig(name, loggingEnabled, maxTTISeconds, maxTTLSeconds, orphanEvictionEnabled, orphanEvictionPeriod, targetMaxInMemoryCount, targetMaxTotalCount, capacityEvictionPolicyDataFactory); if (CacheConfigFactory.DSO_ACTIVE) { return new DistributedCacheImpl<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 V getValueSafe(final TimestampedValue<V> entry) { return entry == null ? null : entry.isExpired(getTime(), config) ? null : entry.getValue(); }