ch.mfrey.thymeleaf.extras.cache.CacheEvictProcessor 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.ProcessorResult;
import org.thymeleaf.processor.attr.AbstractAttrProcessor;
public class CacheEvictProcessor extends AbstractAttrProcessor {
public static final Logger log = LoggerFactory.getLogger(CacheEvictProcessor.class);
public static final int PRECEDENCE = 10;
public CacheEvictProcessor() {
super("evict");
}
@Override
protected ProcessorResult processAttribute(Arguments arguments, Element element, String attributeName) {
final String attributeValue = element.getAttributeValue(attributeName);
element.removeAttribute(attributeName);
final String cacheName = ExpressionSupport.getEvaluatedAttributeValueAsString(arguments, attributeValue);
if (cacheName == "") {
return ProcessorResult.OK;
}
CacheManager.evict(arguments, cacheName);
return ProcessorResult.OK;
}
@Override
public int getPrecedence() {
return CacheProcessor.PRECEDENCE - 1; // Run just before the CacheProcessor
}
}