Skip to content

Commit

Permalink
添加缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuo894 committed Sep 23, 2024
1 parent 725dbb6 commit 911a7ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/main/java/ani/rss/action/WebHookAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public synchronized void doAction(HttpServerRequest request, HttpServerResponse
if (StrUtil.isNotBlank(episodeId)) {
break;
}
ThreadUtil.sleep(1000L);
}
if (StrUtil.isBlank(episodeId)) {
log.info("获取bgm对应剧集失败");
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/ani/rss/util/BgmUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ani.rss.util;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpResponse;
Expand All @@ -11,13 +14,15 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* BGM
*/
public class BgmUtil {
private static final String host = "https://api.bgm.tv";
private static final Gson gson = new Gson();
private static final Cache<String, String> nameCache = CacheUtil.newFIFOCache(64);

/**
* 查找番剧id
Expand All @@ -26,7 +31,15 @@ public class BgmUtil {
* @return 番剧id
*/
public static String getSubjectId(String name) {
return HttpReq.get(host + "/search/subject/" + name, true)
if (StrUtil.isBlank(name)) {
return "";
}

if (nameCache.containsKey(name)) {
return nameCache.get(name);
}

String id = HttpReq.get(host + "/search/subject/" + name, true)
.header("Authorization", "Bearer " + ConfigUtil.CONFIG.getBgmToken())
.form("type", 2)
.form("responseGroup", "small")
Expand Down Expand Up @@ -61,6 +74,9 @@ public static String getSubjectId(String name) {
// 次之使用第一个
return list.get(0).getAsJsonObject().get("id").getAsString();
});
ThreadUtil.sleep(1000);
nameCache.put(name, id, TimeUnit.DAYS.toDays(1));
return id;
}

/**
Expand Down

0 comments on commit 911a7ce

Please sign in to comment.