HtmlCache.java 1.18 KB
package com.bckefu.excel.cache;

import com.bckefu.excel.entity.ExcelToHtmlParams;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.TimeUnit;

/**
 * @author : caoliang
 * @date : 2017/11/20  下午2:48
 */
public class HtmlCache {

    private static LoadingCache<ExcelToHtmlParams, String> loadingCache;

    static {
        loadingCache = CacheBuilder.newBuilder().expireAfterWrite(7, TimeUnit.DAYS).maximumSize(200)
                .build(new CacheLoader<ExcelToHtmlParams, String>() {
                    @Override
                    public String load(ExcelToHtmlParams params) throws Exception {
                        return new ExcelToHtmlServer(params).printPage();
                    }
                });
    }

    public static String getHtml(ExcelToHtmlParams params) {
        try {
            return loadingCache.get(params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static void setLoadingCache(LoadingCache<ExcelToHtmlParams, String> loadingCache) {
        HtmlCache.loadingCache = loadingCache;
    }

}