private String getSourceFromCache(String templateName) { try { return sourceCache.get(templateName).orElse(null); } catch (Exception e) { throw unwrapUncheckedExecutionException(e); } }
/** * The member cache may theoretically cause memory leaks due to using hard * references to {@link Class} and {@link Member} instances. As a temporary * workaround we provide a way to invalidate the cache or some of its * entries (e.g. for a concrete classloader). * * @param predicate * If null, all cache entries are discarded, otherwise an entry * is only discarded if the given predicate returns * <code>true</code> for the {@link MemberKey#getClass()} */ public void invalidateMemberCache(Predicate<Class<?>> predicate) { if (memberCache == null) { return; } if (predicate == null) { memberCache.clear(); } else { memberCache.invalidate((key) -> predicate.test(key.getClazz())); } }
/** * * @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; }
/** * * @return all available simple statistics */ public Set<SimpleStats> getSimpleStats() { if (data.size() == 0) { return Collections.emptySet(); } ImmutableSetBuilder<SimpleStats> buidler = ImmutableSet.builder(); for (Entry<String, ComputingCache<Long, AtomicLong>> entry : data .getAllPresent().entrySet()) { buidler.add(new SimpleStats(entry.getKey(), entry.getValue() .getAllPresent())); } return buidler.build(); }
/** * Drop all the collected data. */ public void clearData() { data.clear(); }
/** * * @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; }
private Map<Long, Long> getImmutableTemplateData( ComputingCache<Long, AtomicLong> templateData) { ImmutableMapBuilder<Long, Long> builder = ImmutableMap.builder(); for (Entry<Long, AtomicLong> entry : templateData.getAllPresent() .entrySet()) { builder.put(entry.getKey(), entry.getValue().get()); } return builder.build(); }
/** * 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)); }
long getMemberCacheSize() { return memberCache != null ? memberCache.size() : 0L; }
/** * * @return all collected data */ public Map<String, Map<Long, Long>> getData() { if (data.size() == 0) { return Collections.emptyMap(); } ImmutableMapBuilder<String, Map<Long, Long>> builder = ImmutableMap .builder(); for (Entry<String, ComputingCache<Long, AtomicLong>> entry : data .getAllPresent().entrySet()) { builder.put(entry.getKey(), getImmutableTemplateData(entry.getValue())); } return builder.build(); }
/** * Invalidate all the cache fragments. */ public void invalidateFragments() { if (fragments == null) { return; } fragments.clear(); }
/** * * @param mustache * @return the raw data for the given template */ public Collection<ExecutionData> getRawData(Mustache mustache) { Checker.checkArgumentNotNull(mustache); ConcurrentMap<Long, ExecutionData> executions = data.getIfPresent(mustache .getGeneratedId()); if (executions != null) { return ImmutableMap.copyOf(executions).values(); } return null; }
/** * * @return the statistics */ public Set<Stats> getStats() { ImmutableSetBuilder<Stats> builder = ImmutableSet.builder(); for (Entry<Long, ConcurrentMap<Long, ExecutionData>> entry : data .getAllPresent().entrySet()) { builder.add(parseData(idsToNames.get(entry.getKey()), entry.getKey(), entry.getValue().values())); } return builder.build(); }
@Override public void invalidateTemplateCache(Predicate<String> predicate) { if (isCacheEnabled()) { checkArgumentNotNull(predicate); templateCache.invalidate(predicate::test); if (sourceCache != null) { sourceCache.invalidate(predicate::test); } } }
@Override protected String format(TemporalAccessor value, FormatStyle style, Locale locale, TimeZone timeZone) { return formatterCache.get(new CacheKey(locale, timeZone, null, style)) .format(value); }
/** * Drop all the collected data. */ public void clearData() { data.clear(); }
@Override public Hint createHint(Object contextObject, String name, ResolutionContext context) { MemberKey key = MemberKey.newInstance(contextObject, name); MemberWrapper wrapper; if (memberCache != null) { Optional<MemberWrapper> found = memberCache.getIfPresent(key); wrapper = found != null ? found.get() : null; } else { wrapper = findWrapper(key).orElse(null); } if (wrapper != null) { return new ReflectionHint(key, wrapper); } // This should never happen return Hints.INAPPLICABLE_HINT; }
@Override protected String format(TemporalAccessor value, String pattern, Locale locale, TimeZone timeZone) { return formatterCache .get(new CacheKey(locale, timeZone, pattern, null)).format( value); }
/** * * @param mustache * @return the statistics for the given template */ public Stats getStats(Mustache mustache) { Checker.checkArgumentNotNull(mustache); ConcurrentMap<Long, ExecutionData> times = data.getIfPresent(mustache .getGeneratedId()); if (times != null) { return parseData(mustache.getName(), mustache.getGeneratedId(), times.values()); } return null; }