diff --git a/backend/package.json b/backend/package.json index 1262ee2..0d68952 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.238", + "version": "2.14.239", "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 b2be338..600f257 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -53,7 +53,8 @@ export default async function download(rawUrl, ua, timeout) { // } const { isNode } = ENV(); - const { defaultUserAgent, defaultTimeout } = $.read(SETTINGS_KEY); + const { defaultUserAgent, defaultTimeout, cacheThreshold } = + $.read(SETTINGS_KEY); const userAgent = ua || defaultUserAgent || 'clash.meta'; const requestTimeout = timeout || defaultTimeout; const id = hex_md5(userAgent + url); @@ -90,8 +91,22 @@ export default async function download(rawUrl, ua, timeout) { } if (body.replace(/\s/g, '').length === 0) throw new Error(new Error('远程资源内容为空')); + let shouldCache = true; + if (cacheThreshold) { + const size = body.length / 1024; + if (size > cacheThreshold) { + $.info( + `资源大小 ${size.toFixed( + 2, + )} KB 超过了 ${cacheThreshold} KB, 不缓存`, + ); + shouldCache = false; + } + } + if (shouldCache) { + resourceCache.set(id, body); + } - resourceCache.set(id, body); result = body; } catch (e) { throw new Error(`无法下载 URL ${url}: ${e.message ?? e}`);