feat: 支持设置并在远程订阅失败时读取最近一次成功的缓存

This commit is contained in:
xream 2024-04-09 20:49:42 +08:00
parent 2a1c2eb9df
commit f4cdc953e6
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
2 changed files with 18 additions and 1 deletions

View File

@ -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": {

View File

@ -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) {
@ -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}`);
}
}