private void notifyListener(K key, RemovalCause cause) { if (listener != null) { listener.entryInvalidated(key, cause.toString()); } }
/** * Drop all the collected data. */ public void clearData() { data.clear(); }
long getMemberCacheSize() { return memberCache != null ? memberCache.size() : 0L; }
@Override public V get(K key) { CacheEntry<V> entry = compute(key); if (entry.isExpired(expirationTimeout)) { if (map.remove(key) != null) { notifyListener(key, RemovalCause.EXPIRED); } entry = compute(key); } return entry.value; }
/** * * @param computingFunction * @param maxSize * @param maxSizeStrategy */ ConcurrentHashMapAdapter( ComputingCache.Function<K, V> computingFunction, Long maxSize, MaxSizeStrategy maxSizeStrategy, Long expirationTimeout, Listener<K> listener) { this.map = new ConcurrentHashMap<>(); this.maxSize = maxSize; this.expirationTimeout = expirationTimeout; this.listener = listener; this.computingFunctionAdapter = (key) -> { if (maxSize != null && map.size() > maxSize) { throw new MaxSizeExceededException(); } return CacheEntry.of(computingFunction.compute(key)); }; this.maxSizeStrategy = maxSizeStrategy; }
private String getSourceFromCache(String templateName) { try { return sourceCache.get(templateName).orElse(null); } catch (Exception e) { throw unwrapUncheckedExecutionException(e); } }
@Override public <K, V> ComputingCache<K, V> create(String consumerId, Function<K, V> computingFunction, Long expirationTimeout, Long maxSize, Listener<K> listener) { if (maxSize != null && !maxSizeStrategy.isEvictionSupported()) { throw new IllegalArgumentException( "Max size limit not supported - use a different eviction strategy"); } return new ConcurrentHashMapAdapter<>(computingFunction, maxSize, maxSizeStrategy, expirationTimeout, listener); }
@Override public V getIfPresent(K key) { CacheEntry<V> entry = map.get(key); if (entry != null) { if (entry.isExpired(expirationTimeout)) { if (map.remove(key) != null) { notifyListener(key, RemovalCause.EXPIRED); } } else { return entry.value; } } return null; }
/** * * @param templateId * @return a simple statistics for the given template */ public SimpleStats getSimpleStats(String templateId) { ComputingCache<Long, AtomicLong> entry = data.getIfPresent(templateId); if (entry != null) { return new SimpleStats(templateId, entry.getAllPresent()); } return null; }
@Override public void invalidate(ComputingCache.KeyPredicate<K> predicate) { for (Iterator<K> iterator = map.keySet().iterator(); iterator .hasNext();) { K key = iterator.next(); if (predicate.apply(key)) { iterator.remove(); notifyListener(key, RemovalCause.INVALIDATE); } } }
/** * Invalidate the cache fragments whose key contains the given part of the * key. * * @param keyPart */ public void invalidateFragments(final String keyPart) { if (fragments == null || keyPart == null) { return; } fragments.invalidate(fragmentKey -> fragmentKey.getKey().contains(keyPart)); }
private synchronized void handleMaxSizeExceeding() { if (map.size() > maxSize) { applyMaxSizeStrategy(); } }
static <T> CacheEntry<T> of(T value) { return new CacheEntry<>(System.currentTimeMillis(), value); }
private CacheEntry<V> compute(K key) { try { return map.computeIfAbsent(key, computingFunctionAdapter); } catch (MaxSizeExceededException e) { handleMaxSizeExceeding(); // Theoretically, this may also throw MaxSizeExceededException // if the limit is exceeded before the value is computed, which // is unlikely. return map.computeIfAbsent(key, computingFunctionAdapter); } }
/** * * @param templateId * @return data for the given template */ public Map<Long, Long> getData(String templateId) { ComputingCache<Long, AtomicLong> templateData = data .getIfPresent(templateId); if (templateData != null) { return getImmutableTemplateData(templateData); } return null; }
@Override protected String format(TemporalAccessor value, String pattern, Locale locale, TimeZone timeZone) { return formatterCache .get(new CacheKey(locale, timeZone, pattern, null)).format( value); }
/** * Invalidate all the cache fragments. */ public void invalidateFragments() { if (fragments == null) { return; } fragments.clear(); }
@Override public void invalidateTemplateCache(Predicate<String> predicate) { if (isCacheEnabled()) { checkArgumentNotNull(predicate); templateCache.invalidate(predicate::test); if (sourceCache != null) { sourceCache.invalidate(predicate::test); } } }
/** * Drop all the collected data. */ public void clearData() { data.clear(); }