Skip to content

Commit

Permalink
feat: 支持设置并在远程订阅失败时读取最近一次成功的缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Apr 9, 2024
1 parent 2a1c2eb commit f4cdc95
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.280",
"version": "2.14.281",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
17 changes: 17 additions & 0 deletions backend/src/utils/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export default async function download(rawUrl, ua, timeout, proxy) {
}
}
}
const customCacheKey = $arguments?.cacheKey
? `#sub-store-cached-custom-${$arguments?.cacheKey}`
: undefined;

// const downloadUrlMatch = url.match(/^\/api\/(file|module)\/(.+)/);
// if (downloadUrlMatch) {
Expand Down Expand Up @@ -116,10 +119,24 @@ export default async function download(rawUrl, ua, timeout, proxy) {
}
if (shouldCache) {
resourceCache.set(id, body);
if (customCacheKey) {
$.write(body, customCacheKey);
}
}

result = body;
} catch (e) {
if (customCacheKey) {
const cached = $.read(customCacheKey);
if (cached) {
$.info(
`无法下载 URL ${url}: ${
e.message ?? e
}\n使用自定义缓存 ${$arguments?.cacheKey}`,
);
return cached;
}
}
throw new Error(`无法下载 URL ${url}: ${e.message ?? e}`);
}
}
Expand Down

0 comments on commit f4cdc95

Please sign in to comment.