From b720f657d07ea15dc2d1fa8549ca38827f7e95f4 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 16 Apr 2020 10:59:18 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20=E5=B0=86=E9=83=A8=E5=88=86CacheStor?= =?UTF-8?q?e=E5=AF=B9=E8=B1=A1=E7=9A=84=E7=B1=BB=E5=9E=8B=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E4=B8=BACacheStore<>=E4=BB=A5=E4=BF=9D=E8=AF=81DIP,?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E7=81=B5=E6=B4=BB=E6=80=A7;=20[Change]=20?= =?UTF-8?q?=E5=B0=86Ranking=E7=BC=93=E5=AD=98=E8=B0=83=E6=95=B4=E4=B8=BAJs?= =?UTF-8?q?onObjectRedisListCacheStore,=20=E5=87=8F=E5=B0=91=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/BotCommandProcess.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java index 84a0700..b5e86bd 100644 --- a/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java +++ b/src/main/java/net/lamgc/cgj/bot/BotCommandProcess.java @@ -47,10 +47,10 @@ public class BotCommandProcess { .create(); private final static Hashtable imageCache = new Hashtable<>(); - private final static JsonRedisCacheStore illustInfoCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson); - private final static JsonRedisCacheStore illustPreLoadDataCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson); - private final static JsonRedisCacheStore searchBodyCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "searchBody", gson); - private final static JsonRedisCacheStore rankingCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "ranking", gson); + private final static CacheStore illustInfoCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "illustInfo", gson); + private final static CacheStore illustPreLoadDataCache = new JsonRedisCacheStore(BotEventHandler.redisServer, "illustPreLoadData", gson); + 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 RedisPoolCacheStore>(BotEventHandler.redisServer, "imagePages") { @Override protected String parse(List dataObj) { @@ -194,7 +194,7 @@ public class BotCommandProcess { } //TODO(LamGC, 2020.4.11): 将JsonRedisCacheStore更改为使用Redis的List集合, 以提高性能 - List rankingInfoList = getRankingInfoByCache(type, mode, queryDate, 0, Math.max(0, itemLimit), false); + List rankingInfoList = getRankingInfoByCache(type, mode, queryDate, 1, Math.max(0, itemLimit), false); if(rankingInfoList.isEmpty()) { return "无法查询排行榜,可能排行榜尚未更新。"; } @@ -679,31 +679,29 @@ public class BotCommandProcess { String date = new SimpleDateFormat("yyyyMMdd").format(queryDate); String requestSign = buildSyncKey(contentType.name(), ".", mode.name(), ".", date); - JsonArray result = null; + List result = null; if(!rankingCache.exists(requestSign) || flushCache) { synchronized(requestSign) { if(!rankingCache.exists(requestSign) || flushCache) { log.info("Ranking缓存失效, 正在更新...(RequestSign: {})", requestSign); List rankingResult = pixivDownload.getRanking(contentType, mode, queryDate, 1, 500); - JsonArray rankingArr = new JsonArray(rankingResult.size()); - rankingResult.forEach(rankingArr::add); - if(rankingArr.size() == 0) { + if(rankingResult.size() == 0) { log.info("数据获取失败, 将设置浮动有效时间以准备下次更新."); } - - result = rankingArr; - rankingCache.update(requestSign, rankingArr, - rankingArr.size() == 0 ? 5400000 + expireTimeFloatRandom.nextInt(1800000) : 0); + result = new ArrayList<>(rankingResult).subList(start - 1, range); + rankingCache.update(requestSign, rankingResult, + rankingResult.size() == 0 ? 5400000 + expireTimeFloatRandom.nextInt(1800000) : 0); log.info("Ranking缓存更新完成.(RequestSign: {})", requestSign); } } } if (Objects.isNull(result)) { - result = rankingCache.getCache(requestSign).getAsJsonArray(); + result = rankingCache.getCache(requestSign, start - 1, range); log.debug("RequestSign [{}] 缓存命中.", requestSign); } - return PixivDownload.getRanking(result, start, range); + log.debug("Result-Length: {}", result.size()); + return PixivDownload.getRanking(result, start - 1, range); } private static String buildSyncKey(String... keys) {