From e6d1aa1150680a1ac5934a2e5b16020f1df5fb47 Mon Sep 17 00:00:00 2001 From: xream Date: Mon, 13 May 2024 17:08:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E4=BA=86=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=BC=93=E5=AD=98=20cacheKey=20=E7=9A=84?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E8=AE=A2=E9=98=85=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=B8=BA=E4=B9=90=E8=A7=82=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/utils/download.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/backend/package.json b/backend/package.json index cc8dfa7..c52ee32 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.309", + "version": "2.14.310", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index 12f4b2a..6328106 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -14,7 +14,13 @@ import $ from '@/core/app'; const tasks = new Map(); -export default async function download(rawUrl, ua, timeout, proxy) { +export default async function download( + rawUrl, + ua, + timeout, + proxy, + skipCustomCache, +) { let $arguments = {}; let url = rawUrl.replace(/#noFlow$/, ''); const rawArgs = url.split('#'); @@ -39,6 +45,23 @@ export default async function download(rawUrl, ua, timeout, proxy) { ? `#sub-store-cached-custom-${$arguments?.cacheKey}` : undefined; + if (customCacheKey && !skipCustomCache) { + const cached = $.read(customCacheKey); + if (cached) { + $.info( + `乐观缓存: URL ${url}\n本次返回自定义缓存 ${$arguments?.cacheKey}\n并进行请求 尝试更新缓存`, + ); + download( + rawUrl.replace(/(\?|&)cacheKey=.*?(&|$)/, ''), + ua, + timeout, + proxy, + true, + ); + return cached; + } + } + // const downloadUrlMatch = url.match(/^\/api\/(file|module)\/(.+)/); // if (downloadUrlMatch) { // let type = downloadUrlMatch?.[1]; @@ -81,7 +104,7 @@ export default async function download(rawUrl, ua, timeout, proxy) { // try to find in app cache const cached = resourceCache.get(id); - if (!$arguments?.noCache && cached) { + if (!$arguments?.noCache && cached && !skipCustomCache) { $.info(`使用缓存: ${url}`); result = cached; } else { @@ -120,6 +143,9 @@ export default async function download(rawUrl, ua, timeout, proxy) { if (shouldCache) { resourceCache.set(id, body); if (customCacheKey) { + $.info( + `URL ${url}\n写入自定义缓存 ${$arguments?.cacheKey}`, + ); $.write(body, customCacheKey); } }