All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.mfrey.thymeleaf.extras.cache.CacheManager Maven / Gradle / Ivy

Go to download

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)

There is a newer version: 3.0.1
Show newest version
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);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy