diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/HtmlCache.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/HtmlCache.java index 4cb07a7..92ae10d 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/HtmlCache.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/HtmlCache.java @@ -1,44 +1,22 @@ package cn.afterturn.easypoi.cache; -import java.util.concurrent.TimeUnit; - -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; - import cn.afterturn.easypoi.excel.entity.ExcelToHtmlParams; import cn.afterturn.easypoi.excel.html.ExcelToHtmlService; /** * Excel 转变成为Html 的缓存 + * * @author JueYue - * 2015年8月7日 下午1:29:47 + * 2015年8月7日 下午1:29:47 */ public class HtmlCache { - private static LoadingCache loadingCache; - - static { - loadingCache = CacheBuilder.newBuilder().expireAfterWrite(7, TimeUnit.DAYS).maximumSize(200) - .build(new CacheLoader() { - @Override - public String load(ExcelToHtmlParams params) throws Exception { - return new ExcelToHtmlService(params).printPage(); - } - }); - } - public static String getHtml(ExcelToHtmlParams params) { try { - return loadingCache.get(params); + return new ExcelToHtmlService(params).printPage(); } catch (Exception e) { throw new RuntimeException(e); } } - - public static void setLoadingCache(LoadingCache loadingCache) { - HtmlCache.loadingCache = loadingCache; - } - } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/ImageCache.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/ImageCache.java index cb7158e..ea90275 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/ImageCache.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/ImageCache.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * 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 + *

+ * 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 @@ -18,58 +18,43 @@ package cn.afterturn.easypoi.cache; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.poi.util.IOUtils; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; - import cn.afterturn.easypoi.cache.manager.POICacheManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * 图片缓存处理 + * * @author JueYue - * 2016年1月8日 下午4:16:32 + * 2016年1月8日 下午4:16:32 */ public class ImageCache { - private static LoadingCache loadingCache; - - static { - loadingCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS) - .maximumSize(2000).build(new CacheLoader() { - @Override - public byte[] load(String imagePath) throws Exception { - InputStream is = POICacheManager.getFile(imagePath); - BufferedImage bufferImg = ImageIO.read(is); - ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); - try { - ImageIO.write(bufferImg, - imagePath.substring(imagePath.indexOf(".") + 1, imagePath.length()), - byteArrayOut); - return byteArrayOut.toByteArray(); - } finally { - IOUtils.closeQuietly(is); - IOUtils.closeQuietly(byteArrayOut); - } - } - }); - } + private static final Logger LOGGER = LoggerFactory + .getLogger(ImageCache.class); public static byte[] getImage(String imagePath) { + InputStream is = POICacheManager.getFile(imagePath); + ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); try { - return loadingCache.get(imagePath); + BufferedImage bufferImg = ImageIO.read(is); + ImageIO.write(bufferImg, + imagePath.substring(imagePath.indexOf(".") + 1, imagePath.length()), + byteArrayOut); + return byteArrayOut.toByteArray(); } catch (Exception e) { + LOGGER.error(e.getMessage(), e); return null; + } finally { + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(byteArrayOut); } } - - public static void setLoadingCache(LoadingCache loadingCache) { - ImageCache.loadingCache = loadingCache; - } + } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java index 1936361..71d0202 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java @@ -26,8 +26,9 @@ import java.net.URLConnection; /** * 文件加载类,根据路径加载指定文件 + * * @author JueYue - * 2014年2月10日 + * 2014年2月10日 * @version 1.0 */ public class FileLoaderImpl implements IFileLoader { @@ -66,7 +67,7 @@ public class FileLoaderImpl implements IFileLoader { } baos.flush(); return baos.toByteArray(); - } catch (IOException e) { + } catch (Exception e) { LOGGER.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(fileis); diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/POICacheManager.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/POICacheManager.java index 3948ba5..50efe78 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/POICacheManager.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/POICacheManager.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * 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 + *

+ * 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 @@ -18,19 +18,13 @@ package cn.afterturn.easypoi.cache.manager; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Arrays; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; - /** * 缓存管理 - * + * * @author JueYue * 2014年2月10日 * 2015年10月17日 @@ -38,57 +32,41 @@ import com.google.common.cache.LoadingCache; */ public final class POICacheManager { - private static final Logger LOGGER = LoggerFactory - .getLogger(POICacheManager.class); - - private static LoadingCache loadingCache; + private static final Logger LOGGER = LoggerFactory + .getLogger(POICacheManager.class); - private static IFileLoader fileLoder; + private static IFileLoader fileLoader = new FileLoaderImpl(); - private static ThreadLocal LOCAL_FILELOADER = new ThreadLocal(); - - static { - loadingCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).maximumSize(50) - .build(new CacheLoader() { - @Override - public byte[] load(String url) throws Exception { - if (LOCAL_FILELOADER.get() != null) { - return LOCAL_FILELOADER.get().getFile(url); - } - return fileLoder.getFile(url); - } - }); - //设置默认实现 - fileLoder = new FileLoaderImpl(); - } + private static ThreadLocal LOCAL_FILELOADER = new ThreadLocal(); public static InputStream getFile(String id) { try { + byte[] result; //复杂数据,防止操作原数据 - byte[] result = Arrays.copyOf(loadingCache.get(id), loadingCache.get(id).length); + if (LOCAL_FILELOADER.get() != null) { + result = LOCAL_FILELOADER.get().getFile(id); + } + result = fileLoader.getFile(id); + result = Arrays.copyOf(result, result.length); return new ByteArrayInputStream(result); - } catch (ExecutionException e) { + } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return null; } - public static void setFileLoder(IFileLoader fileLoder) { - POICacheManager.fileLoder = fileLoder; + public static void setFileLoader(IFileLoader fileLoader) { + POICacheManager.fileLoader = fileLoader; } /** * 一次线程有效 - * @param fileLoder + * @param fileLoader */ - public static void setFileLoderOnce(IFileLoader fileLoder) { - if (fileLoder != null) { - LOCAL_FILELOADER.set(fileLoder); + public static void setFileLoaderOnce(IFileLoader fileLoader) { + if (fileLoader != null) { + LOCAL_FILELOADER.set(fileLoader); } } - public static void setLoadingCache(LoadingCache loadingCache) { - POICacheManager.loadingCache = loadingCache; - } - }