ch.mfrey.thymeleaf.extras.cache.CacheManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf-cache-dialect Show documentation
Show all versions of thymeleaf-cache-dialect Show documentation
A dialect for Thymeleaf that allows you to do partial page caching.
Some parts of our webpage will never change during the lifetime of the application or a usersession, but the part should still be dynamic in the beginning.
Working with Thymeleaf 3.0.0+ (3.0.0.RELEASE and its dependencies included)
package ch.mfrey.thymeleaf.extras.cache;
import java.util.List;
import org.thymeleaf.Arguments;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.cache.ICache;
import org.thymeleaf.dom.Node;
public class CacheManager {
private static String getCacheName(final String templateMode, final String name) {
return CacheDialect.CACHE_PREFIX + templateMode + "_" + name;
}
private static ICache> getCache(final TemplateEngine templateEngine) {
if (templateEngine == null) {
throw new RuntimeException("Template Engine cannot be null");
}
return templateEngine.getCacheManager().getFragmentCache();
}
public static List get(final Arguments arguments, final String cacheName, final int cacheTTLs) {
return get(getCache(arguments.getTemplateEngine()), arguments.getTemplateResolution().getTemplateMode(), cacheName,
cacheTTLs);
}
public static List get(final ICache> cache, final String templateMode,
final String cacheName, final int cacheTTLs) {
if (cacheTTLs == 0) {
return cache.get(getCacheName(templateMode, cacheName));
} else {
return cache.get(getCacheName(templateMode, cacheName), new TTLCacheValidityChecker(cacheTTLs));
}
}
public static void put(final Arguments arguments, final String cacheName, final List content) {
put(getCache(arguments.getTemplateEngine()), arguments.getTemplateResolution().getTemplateMode(), cacheName,
content);
}
public static void put(final ICache> cache, final String templateMode, final String cacheName,
final List content) {
cache.put(getCacheName(templateMode, cacheName), content);
}
public static void evict(final ICache> cache, final String templateMode, final String cacheName) {
cache.clearKey(getCacheName(templateMode, cacheName));
}
public static void evict(final Arguments arguments, final String cacheName) {
evict(getCache(arguments.getTemplateEngine()), arguments.getTemplateResolution().getTemplateMode(), cacheName);
}
}