mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-10 07:19:01 +08:00
feat: 进一步优化乐观缓存和同步配置的逻辑
This commit is contained in:
parent
7b783c1fe3
commit
cf82764171
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.14.327",
|
||||
"version": "2.14.328",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
|
||||
"main": "src/main.js",
|
||||
"scripts": {
|
||||
|
@ -117,6 +117,7 @@ async function doSync() {
|
||||
await produceArtifact({
|
||||
type: 'subscription',
|
||||
name: subName,
|
||||
awaitCustomCache: true,
|
||||
});
|
||||
} catch (e) {
|
||||
// $.error(`${e.message ?? e}`);
|
||||
|
@ -36,6 +36,7 @@ async function produceArtifact({
|
||||
produceType,
|
||||
produceOpts = {},
|
||||
subscription,
|
||||
awaitCustomCache,
|
||||
}) {
|
||||
platform = platform || 'JSON';
|
||||
|
||||
@ -67,6 +68,8 @@ async function produceArtifact({
|
||||
ua || sub.ua,
|
||||
undefined,
|
||||
sub.proxy,
|
||||
undefined,
|
||||
awaitCustomCache,
|
||||
);
|
||||
} catch (err) {
|
||||
errors[url] = err;
|
||||
@ -112,6 +115,8 @@ async function produceArtifact({
|
||||
ua || sub.ua,
|
||||
undefined,
|
||||
sub.proxy,
|
||||
undefined,
|
||||
awaitCustomCache,
|
||||
);
|
||||
} catch (err) {
|
||||
errors[url] = err;
|
||||
|
@ -20,6 +20,7 @@ export default async function download(
|
||||
timeout,
|
||||
proxy,
|
||||
skipCustomCache,
|
||||
awaitCustomCache,
|
||||
) {
|
||||
let $arguments = {};
|
||||
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
|
||||
? `#sub-store-cached-custom-${$arguments?.cacheKey}`
|
||||
: undefined;
|
||||
|
||||
if (customCacheKey && !skipCustomCache) {
|
||||
const cached = $.read(customCacheKey);
|
||||
if (cached) {
|
||||
const customCached = $.read(customCacheKey);
|
||||
const cached = resourceCache.get(id);
|
||||
if (!$arguments?.noCache && cached) {
|
||||
$.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;
|
||||
}
|
||||
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)\/(.+)/);
|
||||
@ -83,12 +121,6 @@ export default async function download(
|
||||
// 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)) {
|
||||
return tasks.get(id);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user