Commit f31447f9 by 王祖波

Merge branch 'feature-recommend2' into 'master'

加载图片缓存统计

See merge request !2989
parents 0927e66b 3ac1b6a8
......@@ -10,8 +10,10 @@ import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO;
import com.gic.haoban.manage.service.util.ApolloUtils;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -28,9 +30,7 @@ import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
/**
* Created by wangzubo on 2024/10/27.
......@@ -42,14 +42,15 @@ public class ImageCombined {
private static final Logger logger = LoggerFactory.getLogger(ImageCombined.class);
private static final Cache<String, BufferedImage> imageCache = Caffeine.newBuilder()
.maximumWeight(200 * 1024 * 1024) // 100MB
.weigher((String key, BufferedImage image) -> image.getWidth() * image.getHeight() * 3)
.maximumWeight(300 * 1024 * 1024) // 100MB
.weigher((String key, BufferedImage image) -> image.getWidth() * image.getHeight() * 4)
.removalListener((key, value, cause) -> {
logger.info("移除缓存:{}",key);
if (value != null) {
value.flush();
}
})
.recordStats()
.expireAfterAccess(10, TimeUnit.MINUTES) // 可设置失效时间
.build();
......@@ -62,6 +63,8 @@ public class ImageCombined {
new ThreadPoolExecutor.AbortPolicy()
);
private static final ScheduledExecutorService CACHE_STAT_SCHEDULER = Executors.newScheduledThreadPool(1);
private static void refreshThreadPool(ConfigChangeEvent changeEvent) {
if (!StringUtils.equals(changeEvent.getNamespace(), APPLICATION)) {
return;
......@@ -99,10 +102,19 @@ public class ImageCombined {
ImageIO.scanForPlugins(); // 初始化图像插件
Config config = ConfigService.getAppConfig();
config.addChangeListener(ImageCombined::refreshThreadPool);
CACHE_STAT_SCHEDULER.scheduleAtFixedRate(() -> {
CacheStats stats = imageCache.stats();
logger.info("【加载图片缓存统计】请求数: {}, 命中: {}, 未命中: {}, 命中率: {}%",
stats.requestCount(),
stats.hitCount(),
stats.missCount(),
String.format("%.2f", stats.hitRate() * 100));
}, 10, 10, TimeUnit.MINUTES);
}
public static void main(String[] args) throws Exception{
BufferedImage largeImage = loadImageWithCache(LARGE_IMAGE_URL);
BufferedImage largeImage = loadImageWithCache(LARGE_IMAGE_URL,"");
BufferedImage qrCodeImage = ImageIO.read(new URL(QR_CODE_URL));
int showStyle = 0;
......@@ -112,6 +124,7 @@ public class ImageCombined {
}
public static byte[] getCombinedImage(CombinedQDTO combinedQDTO) throws Exception{
String enterpriseId = combinedQDTO.getEnterpriseId();
String imageUrl = combinedQDTO.getImageUrl();
String qrCodeUrl = combinedQDTO.getQrCodeUrl();
Integer showStyle = combinedQDTO.getShowStyle();
......@@ -124,7 +137,7 @@ public class ImageCombined {
//压缩图片的参数
imageUrl = imageUrl + "?imageView2/2/w/1080/h/10800/format/jpg";
logger.info("加载要合成的图片开始1");
BufferedImage largeImage = loadImageWithCache(imageUrl);
BufferedImage largeImage = loadImageWithCache(imageUrl,enterpriseId);
logger.info("加载要合成的图片开始2");
BufferedImage qrCodeImage = loadImageWithReader(qrCodeUrl);
logger.info("加载要合成的图片结束");
......@@ -170,8 +183,8 @@ public class ImageCombined {
/**
* 从缓存中加载图像
*/
private static BufferedImage loadImageWithCache(String url) {
boolean useCache = ApolloUtils.combinedCache();
private static BufferedImage loadImageWithCache(String url,String enterpriseId) {
boolean useCache = ApolloUtils.combinedCache(enterpriseId);
if (!useCache) {
logger.info("非缓存加载图片:{}", url);
return loadImageWithReader(url);
......
......@@ -69,13 +69,17 @@ public class ApolloUtils {
return Integer.parseInt(combinedFontsType);
}
public static boolean combinedCache() {
public static boolean combinedCache(String enterpriseId) {
Config config = ConfigService.getAppConfig();
String combinedCache = config.getProperty("combinedCache", "");
if (StringUtils.isBlank(combinedCache)) {
return false;
}
return true;
String combinedCacheEid = config.getProperty("combinedCacheEid", "");
if (StringUtils.isBlank(combinedCacheEid)) {
return true;
}
return combinedCacheEid.contains(enterpriseId);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment