From b93944fe8fb25dcee303c84336874529347d3ab3 Mon Sep 17 00:00:00 2001 From: anime Date: Sat, 4 Jan 2025 14:58:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?perf(=E4=BC=98=E5=8C=96Vercel=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=BC=93=E5=AD=98=E8=A1=8C=E4=B8=BA):=20=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=9C=A8Vercel=E7=8E=AF=E5=A2=83=E4=B8=AD=E6=97=A0?= =?UTF-8?q?=E6=84=8F=E4=B9=89=E7=9A=84=E7=BC=93=E5=AD=98=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index f35a1fe1..9430d684 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -2,13 +2,19 @@ import BLOG from '@/blog.config' import FileCache from './local_file_cache' import MemoryCache from './memory_cache' +// 配置是否开启Vercel环境中的缓存,因为Vercel中现有两种缓存方式在无服务环境下基本都是无意义的,纯粹的浪费资源 +const enableCacheInVercel = + process.env.npm_lifecycle_event === 'build' || + process.env.npm_lifecycle_event === 'export' || + !BLOG['isProd'] + /** * 为减少频繁接口请求,notion数据将被缓存 * @param {*} key * @returns */ export async function getDataFromCache(key, force) { - if (BLOG.ENABLE_CACHE || force) { + if (enableCacheInVercel || BLOG.ENABLE_CACHE || force) { const dataFromCache = await getApi().getCache(key) if (!dataFromCache || JSON.stringify(dataFromCache) === '[]') { return null @@ -21,7 +27,7 @@ export async function getDataFromCache(key, force) { } export async function setDataToCache(key, data, customCacheTime) { - if (!data) { + if (!enableCacheInVercel || !data) { return } // console.trace('[API-->>缓存写入]:', key) From 732c16d717d2e67b09275a01dd5a8600ccd11e81 Mon Sep 17 00:00:00 2001 From: anime Date: Sat, 4 Jan 2025 15:06:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(=E4=BD=BF=E7=94=A8const=20=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=BC=95=E5=85=A5=E7=BC=93=E5=AD=98=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=B1=BB):=20=E9=81=BF=E5=85=8D=E5=A4=9A=E6=AC=A1=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index 9430d684..5317d798 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -15,7 +15,7 @@ const enableCacheInVercel = */ export async function getDataFromCache(key, force) { if (enableCacheInVercel || BLOG.ENABLE_CACHE || force) { - const dataFromCache = await getApi().getCache(key) + const dataFromCache = await cacheApi.getCache(key) if (!dataFromCache || JSON.stringify(dataFromCache) === '[]') { return null } @@ -31,24 +31,15 @@ export async function setDataToCache(key, data, customCacheTime) { return } // console.trace('[API-->>缓存写入]:', key) - await getApi().setCache(key, data, customCacheTime) + await cacheApi.setCache(key, data, customCacheTime) } export async function delCacheData(key) { if (!JSON.parse(BLOG.ENABLE_CACHE)) { return } - await getApi().delCache(key) + await cacheApi.delCache(key) } -/** - * 缓存实现类 - * @returns - */ -function getApi() { - if (process.env.ENABLE_FILE_CACHE) { - return FileCache - } else { - return MemoryCache - } -} +// 缓存实现类 +const cacheApi = process.env.ENABLE_FILE_CACHE ? FileCache : MemoryCache