From c06b8717c36b412c5a8961056576dab6c423caff Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 9 May 2020 22:19:29 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20CacheStoreUtils=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E7=BC=93=E5=AD=98=E5=BA=93=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB;=20[Update]=20BotCommandProcess=20=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E6=B3=A8=E9=87=8A,=20=E4=BC=98=E5=8C=96"info"?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=BE=93=E5=87=BA=E5=86=85=E5=AE=B9=E7=9A=84?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/BotCommandProcess.java | 69 ++++++++++++++----- .../lamgc/cgj/bot/cache/CacheStoreUtils.java | 35 ++++++++++ 2 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 src/main/java/net/lamgc/cgj/bot/cache/CacheStoreUtils.java diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index f2fa171..84d124c 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -41,15 +41,46 @@ public class BotCommandProcess { .serializeNulls() .create(); + /* -------------------- 缓存 -------------------- */ + private final static Hashtable imageCache = new Hashtable<>(); - private final static CacheStore illustInfoCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson); - private final static CacheStore illustPreLoadDataCache = new HotDataCacheStore<>( - new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson), - new LocalHashCacheStore<>(), 3600000, 900000, true); - private final static CacheStore searchBodyCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "searchBody", gson); - private final static CacheStore> rankingCache = new JsonObjectRedisListCacheStore(BotEventHandler.redisServer, "ranking", gson); - private final static CacheStore> pagesCache = new StringListRedisCacheStore(BotEventHandler.redisServer, "imagePages"); - public final static CacheStore reportStore = new JsonRedisCacheStore(BotEventHandler.redisServer, "report", gson); + + /** + * 作品信息缓存 - 不过期 + */ + private final static CacheStore illustInfoCache = + new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson); + + /** + * 作品信息预加载数据 - 有效期 2 小时, 本地缓存有效期1 ± 0.25 + */ + private final static CacheStore illustPreLoadDataCache = + CacheStoreUtils.hashLocalHotDataStore( + new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson), + 3600000, 900000); + /** + * 搜索内容缓存, 有效期 2 小时 + */ + private final static CacheStore searchBodyCache = + new JsonRedisCacheStore(BotEventHandler.redisServer, "searchBody", gson); + + /** + * 排行榜缓存, 不过期 + */ + private final static CacheStore> rankingCache = + new JsonObjectRedisListCacheStore(BotEventHandler.redisServer, "ranking", gson); + + /** + * 作品页面下载链接缓存 - 不过期 + */ + private final static CacheStore> pagesCache = + new StringListRedisCacheStore(BotEventHandler.redisServer, "imagePages"); + + /** + * 作品报告存储 - 不过期 + */ + public final static CacheStore reportStore = + new JsonRedisCacheStore(BotEventHandler.redisServer, "report", gson); private final static RankingUpdateTimer updateTimer = new RankingUpdateTimer(); @@ -122,16 +153,18 @@ public class BotCommandProcess { } JsonObject illustPreLoadData = getIllustPreLoadData(illustId, false); - StringBuilder builder = new StringBuilder("---------------- 作品信息 ----------------\n"); - builder.append("作品Id: ").append(illustId).append("\n"); - builder.append("作品标题:").append(illustPreLoadData.get("illustTitle").getAsString()).append("\n"); - builder.append("作者(作者Id):").append(illustPreLoadData.get("userName").getAsString()) - .append("(").append(illustPreLoadData.get("userId").getAsInt()).append(")\n"); - builder.append("点赞数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.LIKE.attrName).getAsInt()).append("\n"); - builder.append("收藏数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.BOOKMARK.attrName).getAsInt()).append("\n"); - builder.append("围观数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.VIEW.attrName).getAsInt()).append("\n"); - builder.append("评论数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.COMMENT.attrName).getAsInt()).append("\n"); - builder.append("页数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.PAGE.attrName).getAsInt()).append("页\n"); + StringBuilder builder = new StringBuilder("色图姬帮你了解了这个作品的信息!\n"); + builder.append("---------------- 作品信息 ----------------"); + builder.append("\n作品Id: ").append(illustId); + builder.append("\n作品标题:").append(illustPreLoadData.get("illustTitle").getAsString()); + builder.append("\n作者(作者Id):").append(illustPreLoadData.get("userName").getAsString()) + .append("(").append(illustPreLoadData.get("userId").getAsInt()).append(")"); + builder.append("\n点赞数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.LIKE.attrName).getAsInt()); + builder.append("\n收藏数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.BOOKMARK.attrName).getAsInt()); + builder.append("\n围观数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.VIEW.attrName).getAsInt()); + builder.append("\n评论数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.COMMENT.attrName).getAsInt()); + builder.append("\n页数:").append(illustPreLoadData.get(PreLoadDataComparator.Attribute.PAGE.attrName).getAsInt()).append("页"); + builder.append("\n作品链接:").append(artworksLink(fromGroup, illustId)).append("\n"); builder.append("---------------- 作品图片 ----------------\n"); builder.append(getImageById(fromGroup, illustId, PixivDownload.PageQuality.REGULAR, 1)).append("\n"); builder.append("使用 \".cgj image -id ") diff --git a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreUtils.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreUtils.java new file mode 100644 index 0000000..5ae199f --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreUtils.java @@ -0,0 +1,35 @@ +package net.lamgc.cgj.bot.cache; + +public final class CacheStoreUtils { + + private CacheStoreUtils() {} + + /** + * 将 CacheStore 转换成 基于 {@link LocalHashCacheStore} 的 {@link HotDataCacheStore} + *

通过该方法转换, 会自动启用 自动清理

+ * @param cacheStore 上游缓存库 + * @param expireTime 热点缓存最小有效期 + * @param floatRange 缓存浮动最大范围 + * @param 缓存库数据类型 + * @return 返回 {@link HotDataCacheStore} + */ + public static CacheStore hashLocalHotDataStore(CacheStore cacheStore, long expireTime, int floatRange) { + return hashLocalHotDataStore(cacheStore, expireTime, floatRange, true); + } + + /** + * 将 CacheStore 转换成 基于 {@link LocalHashCacheStore} 的 {@link HotDataCacheStore} + * @param cacheStore 上游缓存库 + * @param expireTime 热点缓存最小有效期 + * @param floatRange 缓存浮动最大范围 + * @param autoClean 是否启用自动清理 + * @param 缓存库数据类型 + * @return 返回 {@link HotDataCacheStore} + */ + public static CacheStore hashLocalHotDataStore(CacheStore cacheStore, + long expireTime, int floatRange, boolean autoClean) { + return new HotDataCacheStore<>(cacheStore, new LocalHashCacheStore<>(), expireTime, floatRange, autoClean); + } + + +}