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

org.thymeleaf.ParsedTemplateCache Maven / Gradle / Ivy

/*
 * =============================================================================
 * 
 *   Copyright (c) 2011, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author Daniel Fernández
 * 
 * @since 1.0
 *
 */
final class ParsedTemplateCache {

    public static final String CACHE_LOGGER_NAME = TemplateEngine.class.getName() + ".PARSERCACHE";
    
    
    protected static final Logger cacheLogger = LoggerFactory.getLogger(CACHE_LOGGER_NAME);
    
    private final CacheMap cache;
    private final ReadWriteLock lock;
    private final Lock rLock;
    private final Lock wLock;
    
    
    
    ParsedTemplateCache(final int cacheSize) {
        super();
        this.cache = new CacheMap(cacheSize);
        this.lock = new ReentrantReadWriteLock();
        this.rLock = this.lock.readLock();
        this.wLock = this.lock.writeLock();
        cacheLogger.info("[THYMELEAF][CACHE_INITIALIZE] Initializing parsed template cache with size: {}", Integer.valueOf(cacheSize));
    }
    
    
    
    ParsedTemplate getParsedTemplate(final String templateName) {

        ParsedTemplate cacheValue = null;
        
        this.rLock.lock();
        try {
            cacheValue = this.cache.get(templateName);
        } finally {
            this.rLock.unlock();
        }
        
        if (cacheValue == null) {
            if (cacheLogger.isDebugEnabled()) {
                cacheLogger.debug("[THYMELEAF][{}][{}][CACHE_MISS] Cache miss for template \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateName, templateName});
            }
            return null;
        }
        if (cacheLogger.isDebugEnabled()) {
            cacheLogger.debug("[THYMELEAF][{}][{}][CACHE_HIT] Cache hit for template \"{}\". Template is already parsed", new Object[] {TemplateEngine.threadIndex(), templateName, templateName});
        }
        return cacheValue;
        
    }
    
    
    
    void putParsedTemplate(final ParsedTemplate templateDocumentResolution) {

        final String templateName = templateDocumentResolution.getTemplateName();
        
        if (cacheLogger.isDebugEnabled()) {
            cacheLogger.debug("[THYMELEAF][{}][{}][CACHE_ADD][{}] Adding cache entry for parsed template \"{}\". New size is {}.", new Object[] {TemplateEngine.threadIndex(), templateName, Integer.valueOf(this.cache.size() + 1), templateName, Integer.valueOf(this.cache.size() + 1)});
        }
        
        this.wLock.lock();
        try {
            this.cache.put(templateName, templateDocumentResolution);
        } finally {
            this.wLock.unlock();
        }
        
    }
    
    
    
    final static class CacheMap extends LinkedHashMap {

        private static final long serialVersionUID = -5181808911951171469L;
        
        private final int templateCacheSize;

        public CacheMap(final int templateCacheSize) {
            super(16, 0.75f, true);
            this.templateCacheSize = templateCacheSize;
        }

        @Override
        protected boolean removeEldestEntry(final Entry eldest) {
            final boolean shouldRemove = size() > this.templateCacheSize;
            if (shouldRemove && cacheLogger.isDebugEnabled()) {
                cacheLogger.debug("[THYMELEAF][{}][{}][CACHE_REMOVE][{}] Parsed template cache size exceeded. Removing entry for template \"{}\". New size is {}.", new Object[] {TemplateEngine.threadIndex(), eldest.getKey(), Integer.valueOf(size() - 1), eldest.getKey(), Integer.valueOf(size() - 1)});
            }
            return shouldRemove;
        }
        
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy