mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-11 08:19:01 +08:00
feat: 进一步优化乐观缓存和同步配置的逻辑
This commit is contained in:
parent
7b783c1fe3
commit
cf82764171
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sub-store",
|
"name": "sub-store",
|
||||||
"version": "2.14.327",
|
"version": "2.14.328",
|
||||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
|
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
|
||||||
"main": "src/main.js",
|
"main": "src/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -117,6 +117,7 @@ async function doSync() {
|
|||||||
await produceArtifact({
|
await produceArtifact({
|
||||||
type: 'subscription',
|
type: 'subscription',
|
||||||
name: subName,
|
name: subName,
|
||||||
|
awaitCustomCache: true,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// $.error(`${e.message ?? e}`);
|
// $.error(`${e.message ?? e}`);
|
||||||
|
@ -36,6 +36,7 @@ async function produceArtifact({
|
|||||||
produceType,
|
produceType,
|
||||||
produceOpts = {},
|
produceOpts = {},
|
||||||
subscription,
|
subscription,
|
||||||
|
awaitCustomCache,
|
||||||
}) {
|
}) {
|
||||||
platform = platform || 'JSON';
|
platform = platform || 'JSON';
|
||||||
|
|
||||||
@ -67,6 +68,8 @@ async function produceArtifact({
|
|||||||
ua || sub.ua,
|
ua || sub.ua,
|
||||||
undefined,
|
undefined,
|
||||||
sub.proxy,
|
sub.proxy,
|
||||||
|
undefined,
|
||||||
|
awaitCustomCache,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
errors[url] = err;
|
errors[url] = err;
|
||||||
@ -112,6 +115,8 @@ async function produceArtifact({
|
|||||||
ua || sub.ua,
|
ua || sub.ua,
|
||||||
undefined,
|
undefined,
|
||||||
sub.proxy,
|
sub.proxy,
|
||||||
|
undefined,
|
||||||
|
awaitCustomCache,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
errors[url] = err;
|
errors[url] = err;
|
||||||
|
@ -20,6 +20,7 @@ export default async function download(
|
|||||||
timeout,
|
timeout,
|
||||||
proxy,
|
proxy,
|
||||||
skipCustomCache,
|
skipCustomCache,
|
||||||
|
awaitCustomCache,
|
||||||
) {
|
) {
|
||||||
let $arguments = {};
|
let $arguments = {};
|
||||||
let url = rawUrl.replace(/#noFlow$/, '');
|
let url = rawUrl.replace(/#noFlow$/, '');
|
||||||
@ -41,29 +42,66 @@ export default async function download(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const { isNode, isStash, isLoon, isShadowRocket, isQX } = ENV();
|
||||||
|
const { defaultUserAgent, defaultTimeout, cacheThreshold } =
|
||||||
|
$.read(SETTINGS_KEY);
|
||||||
|
const userAgent = ua || defaultUserAgent || 'clash.meta';
|
||||||
|
const requestTimeout = timeout || defaultTimeout;
|
||||||
|
const id = hex_md5(userAgent + url);
|
||||||
|
|
||||||
const customCacheKey = $arguments?.cacheKey
|
const customCacheKey = $arguments?.cacheKey
|
||||||
? `#sub-store-cached-custom-${$arguments?.cacheKey}`
|
? `#sub-store-cached-custom-${$arguments?.cacheKey}`
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
if (customCacheKey && !skipCustomCache) {
|
if (customCacheKey && !skipCustomCache) {
|
||||||
const cached = $.read(customCacheKey);
|
const customCached = $.read(customCacheKey);
|
||||||
if (cached) {
|
const cached = resourceCache.get(id);
|
||||||
|
if (!$arguments?.noCache && cached) {
|
||||||
$.info(
|
$.info(
|
||||||
`乐观缓存: URL ${url}\n本次返回自定义缓存 ${$arguments?.cacheKey}\n并进行请求 尝试更新缓存`,
|
`乐观缓存: URL ${url}\n存在有效的常规缓存\n使用常规缓存以避免重复请求`,
|
||||||
);
|
);
|
||||||
download(
|
|
||||||
rawUrl.replace(/(\?|&)cacheKey=.*?(&|$)/, ''),
|
|
||||||
ua,
|
|
||||||
timeout,
|
|
||||||
proxy,
|
|
||||||
true,
|
|
||||||
).catch((e) => {
|
|
||||||
$.error(
|
|
||||||
`乐观缓存: URL ${url} 更新缓存发生错误 ${e.message ?? e}`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
if (customCached) {
|
||||||
|
if (awaitCustomCache) {
|
||||||
|
$.info(`乐观缓存: URL ${url}\n本次进行请求 尝试更新缓存`);
|
||||||
|
try {
|
||||||
|
await download(
|
||||||
|
rawUrl.replace(/(\?|&)cacheKey=.*?(&|$)/, ''),
|
||||||
|
ua,
|
||||||
|
timeout,
|
||||||
|
proxy,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
$.error(
|
||||||
|
`乐观缓存: URL ${url} 更新缓存发生错误 ${
|
||||||
|
e.message ?? e
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
$.info('使用乐观缓存的数据刷新缓存, 防止后续请求');
|
||||||
|
resourceCache.set(id, customCached);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$.info(
|
||||||
|
`乐观缓存: URL ${url}\n本次返回自定义缓存 ${$arguments?.cacheKey}\n并进行请求 尝试异步更新缓存`,
|
||||||
|
);
|
||||||
|
download(
|
||||||
|
rawUrl.replace(/(\?|&)cacheKey=.*?(&|$)/, ''),
|
||||||
|
ua,
|
||||||
|
timeout,
|
||||||
|
proxy,
|
||||||
|
true,
|
||||||
|
).catch((e) => {
|
||||||
|
$.error(
|
||||||
|
`乐观缓存: URL ${url} 异步更新缓存发生错误 ${
|
||||||
|
e.message ?? e
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return customCached;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// const downloadUrlMatch = url.match(/^\/api\/(file|module)\/(.+)/);
|
// const downloadUrlMatch = url.match(/^\/api\/(file|module)\/(.+)/);
|
||||||
@ -83,12 +121,6 @@ export default async function download(
|
|||||||
// return item.content;
|
// return item.content;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
const { isNode, isStash, isLoon, isShadowRocket, isQX } = ENV();
|
|
||||||
const { defaultUserAgent, defaultTimeout, cacheThreshold } =
|
|
||||||
$.read(SETTINGS_KEY);
|
|
||||||
const userAgent = ua || defaultUserAgent || 'clash.meta';
|
|
||||||
const requestTimeout = timeout || defaultTimeout;
|
|
||||||
const id = hex_md5(userAgent + url);
|
|
||||||
if (!isNode && tasks.has(id)) {
|
if (!isNode && tasks.has(id)) {
|
||||||
return tasks.get(id);
|
return tasks.get(id);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user