From 437297b8b0c4c59d7dd1a92e2654ca8c099e3936 Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 5 Mar 2024 05:03:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=98=88=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/utils/download.js | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) 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}`);