public static Translation getFromCache(TranslatedEntity entityType, String id, String propertyName, String localeCode) { Map<String, Translation> threadlocalCache = getThreadlocalCache(); Translation translation = threadlocalCache.get(buildCacheKey(entityType, id, propertyName, localeCode)); if (translation == null && StringUtils.contains(localeCode, '_')) { String languageWithoutCountryCode = localeCode.substring(localeCode.indexOf('_') + 1); translation = threadlocalCache.get(buildCacheKey(entityType, id, propertyName, languageWithoutCountryCode)); } return translation; }
@Override public ExtensionResultStatusType endBatchEvent(List<? extends Indexable> indexables) { TranslationBatchReadCache.clearCache(); return ExtensionResultStatusType.HANDLED_CONTINUE; }
protected static Map<String, Translation> getThreadlocalCache() { long threadId = Thread.currentThread().getId(); Element cacheElement = getCache().get(threadId); return cacheElement == null ? null : (Map<String, Translation>) cacheElement.getObjectValue(); }
public static void addToCache(List<Translation> translations) { long threadId = Thread.currentThread().getId(); Map<String, Translation> threadlocalCache = getThreadlocalCache(); if (threadlocalCache == null) { threadlocalCache = new HashMap<String, Translation>(); } Map<String, Translation> additionalTranslations = BLCMapUtils.keyedMap(translations, new TypedClosure<String, Translation>() { @Override public String getKey(Translation translation) { return buildCacheKey(translation); } }); threadlocalCache.putAll(additionalTranslations); getCache().put(new Element(threadId, threadlocalCache)); }
if (TranslationBatchReadCache.hasCache()) { Translation translation = TranslationBatchReadCache.getFromCache(entityType, entityId, property, localeCountryCode); if (translation != null) { return translation.getTranslatedValue();
@Override public String getKey(Translation translation) { return buildCacheKey(translation); } });
public static boolean hasCache() { return getThreadlocalCache() != null; }
protected void addEntitiesToTranslationCache(List<String> entityIds, TranslatedEntity translatedEntity) { List<Translation> translations = translationService.findAllTranslationEntries(translatedEntity, ResultType.STANDARD, entityIds); TranslationBatchReadCache.addToCache(translations); }
protected static String buildCacheKey(Translation translation) { return buildCacheKey(translation.getEntityType(), translation.getEntityId(), translation.getFieldName(), translation.getLocaleCode()); }
public static void clearCache() { long threadId = Thread.currentThread().getId(); getCache().remove(threadId); }