From 5ad1934366d415b378733c9463912e10bac891cd Mon Sep 17 00:00:00 2001 From: tlyong1992 Date: Tue, 31 May 2022 09:16:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=BC=80=E5=8F=91=E3=80=81?= =?UTF-8?q?=E5=BC=80=E5=90=AF=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 17 ++++++++++++----- lib/cache/local_file_cache.js | 8 +++++--- lib/cache/memory_cache.js | 8 +++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index 28801255..4a667df4 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -1,7 +1,14 @@ -import { getCacheFromMemory as getCache, setCacheToMemory as setCache, delCacheFromMemory as delCache } from '@/lib/cache/memory_cache' -// import { getCacheFromFile as getCache, setCacheToFile as setCache, delCacheFromFile as delCache } from './local_file_cache' +import MemoryCache from './memory_cache' +import FileCache from './local_file_cache' const enableCache = true +let api +if (process.env.ENABLE_FILE_CACHE) { + api = FileCache +} else { + api = MemoryCache +} + /** * 为减少频繁接口请求,notion数据将被缓存 * @param {*} key @@ -11,7 +18,7 @@ export async function getDataFromCache(key) { if (!enableCache) { return null } - const dataFromCache = await getCache(key) + const dataFromCache = await api.getCache(key) if (JSON.stringify(dataFromCache) === '[]') { return null } @@ -22,12 +29,12 @@ export async function setDataToCache(key, data) { if (!enableCache || !data) { return } - await setCache(key, data) + await api.setCache(key, data) } export async function delCacheData(key) { if (!enableCache) { return } - await delCache(key) + await api.delCache(key) } diff --git a/lib/cache/local_file_cache.js b/lib/cache/local_file_cache.js index 390c3c66..cda7f35a 100644 --- a/lib/cache/local_file_cache.js +++ b/lib/cache/local_file_cache.js @@ -6,7 +6,7 @@ const cacheInvalidSeconds = 1000000000 * 1000 // 文件名 const jsonFile = path.resolve('./data.json') -export async function getCacheFromFile (key) { +export async function getCache (key) { const exist = await fs.existsSync(jsonFile) if (!exist) return null const data = await fs.readFileSync(jsonFile) @@ -33,7 +33,7 @@ export async function getCacheFromFile (key) { * @param data * @returns {Promise} */ -export async function setCacheToFile (key, data) { +export async function setCache (key, data) { const exist = await fs.existsSync(jsonFile) const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {} json[key] = data @@ -41,10 +41,12 @@ export async function setCacheToFile (key, data) { fs.writeFileSync(jsonFile, JSON.stringify(json)) } -export async function delCacheFromFile (key, data) { +export async function delCache (key, data) { const exist = await fs.existsSync(jsonFile) const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {} delete json.key json[key + '_expire_time'] = new Date().getTime() fs.writeFileSync(jsonFile, JSON.stringify(json)) } + +export default { getCache, setCache, delCache } diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index 5bfee605..1a7012f9 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -3,14 +3,16 @@ import BLOG from 'blog.config' const cacheTime = BLOG.isProd ? 10 * 60 : 120 * 60 // 120 minutes for dev,10 minutes for prod -export async function getCacheFromMemory(key, options) { +export async function getCache(key, options) { return await cache.get(key) } -export async function setCacheToMemory(key, data) { +export async function setCache(key, data) { await cache.put(key, data, cacheTime * 1000) } -export async function delCacheFromMemory(key) { +export async function delCache(key) { await cache.del(key) } + +export default { getCache, setCache, delCache }