From cf82764171c9b46a0872f5e341ad6266160dcb1a Mon Sep 17 00:00:00 2001 From: xream Date: Sat, 1 Jun 2024 19:50:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=B9=90=E8=A7=82=E7=BC=93=E5=AD=98=E5=92=8C=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E9=85=8D=E7=BD=AE=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/products/cron-sync-artifacts.js | 1 + backend/src/restful/sync.js | 5 ++ backend/src/utils/download.js | 72 +++++++++++++++------ 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/backend/package.json b/backend/package.json index 3cd4bb178..a3c476846 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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": { diff --git a/backend/src/products/cron-sync-artifacts.js b/backend/src/products/cron-sync-artifacts.js index 77bc90d91..a3fadf947 100644 --- a/backend/src/products/cron-sync-artifacts.js +++ b/backend/src/products/cron-sync-artifacts.js @@ -117,6 +117,7 @@ async function doSync() { await produceArtifact({ type: 'subscription', name: subName, + awaitCustomCache: true, }); } catch (e) { // $.error(`${e.message ?? e}`); diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index 18e68aa9f..1e07a55c1 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -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; diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index f3cfb221a..4cc68955c 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -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); }