소스 검색

删除坑爹缓存

4.1.3.A
jueyue 6 년 전
부모
커밋
ed17f14613
4개의 변경된 파일58개의 추가작업 그리고 116개의 파일을 삭제
  1. +3
    -25
      easypoi-base/src/main/java/cn/afterturn/easypoi/cache/HtmlCache.java
  2. +26
    -41
      easypoi-base/src/main/java/cn/afterturn/easypoi/cache/ImageCache.java
  3. +3
    -2
      easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/FileLoaderImpl.java
  4. +26
    -48
      easypoi-base/src/main/java/cn/afterturn/easypoi/cache/manager/POICacheManager.java

+ 3
- 25
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<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 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<ExcelToHtmlParams, String> loadingCache) {
HtmlCache.loadingCache = loadingCache;
}
}

+ 26
- 41
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
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, byte[]> loadingCache;
static {
loadingCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS)
.maximumSize(2000).build(new CacheLoader<String, byte[]>() {
@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<String, byte[]> loadingCache) {
ImageCache.loadingCache = loadingCache;
}
}

+ 3
- 2
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);


+ 26
- 48
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
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, byte[]> loadingCache;
private static final Logger LOGGER = LoggerFactory
.getLogger(POICacheManager.class);
private static IFileLoader fileLoder;
private static IFileLoader fileLoader = new FileLoaderImpl();
private static ThreadLocal<IFileLoader> LOCAL_FILELOADER = new ThreadLocal<IFileLoader>();
static {
loadingCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).maximumSize(50)
.build(new CacheLoader<String, byte[]>() {
@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<IFileLoader> LOCAL_FILELOADER = new ThreadLocal<IFileLoader>();
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<String, byte[]> loadingCache) {
POICacheManager.loadingCache = loadingCache;
}
}

불러오는 중...
취소
저장