From 63e39fa2a844b84b9409c1a04e77796fa4fe251c Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Thu, 27 Jan 2022 13:58:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cache/cache_manager.js | 11 +++++++++-- lib/cache/local_file_cache.js | 8 ++++++++ lib/cache/memory_cache.js | 4 ++++ lib/notion/getAllPosts.js | 3 +++ lib/notion/getNotionData.js | 2 +- lib/notion/getPostBlocks.js | 2 +- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index dc978145..90b697f5 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -1,5 +1,5 @@ -import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache' -// import { getCacheFromFile, setCacheToFile } from './local_file_cache' +import { getCacheFromMemory, setCacheToMemory, delCacheFromMemory } from '@/lib/cache/memory_cache' +// import { getCacheFromFile, setCacheToFile, delCacheFromFile } from './local_file_cache' const enableCache = true // 生产环境禁用 /** @@ -24,3 +24,10 @@ export async function setDataToCache (key, data) { } await setCacheToMemory(key, data) } + +export async function delCacheData (key) { + if (!enableCache) { + return + } + await delCacheFromMemory(key) +} diff --git a/lib/cache/local_file_cache.js b/lib/cache/local_file_cache.js index 75ee4f7d..390c3c66 100644 --- a/lib/cache/local_file_cache.js +++ b/lib/cache/local_file_cache.js @@ -40,3 +40,11 @@ export async function setCacheToFile (key, data) { json[key + '_expire_time'] = new Date().getTime() fs.writeFileSync(jsonFile, JSON.stringify(json)) } + +export async function delCacheFromFile (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)) +} diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index 6965dbd7..f0ff0eec 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -10,3 +10,7 @@ export async function getCacheFromMemory (key, options) { export async function setCacheToMemory (key, data) { await cache.put(key, data, cacheTime * 1000) } + +export async function delCacheFromMemory (key) { + await cache.del(key) +} diff --git a/lib/notion/getAllPosts.js b/lib/notion/getAllPosts.js index 7473a4d8..246c5c3f 100644 --- a/lib/notion/getAllPosts.js +++ b/lib/notion/getAllPosts.js @@ -3,6 +3,7 @@ import getAllPageIds from './getAllPageIds' import getPageProperties from './getPageProperties' import { defaultMapImageUrl } from 'react-notion-x' import { getNotionPageData } from '@/lib/notion/getNotionData' +import { delCacheData } from '@/lib/cache/cache_manager' /** * 获取所有文章列表 @@ -61,6 +62,8 @@ export async function getAllPosts ({ notionPageData, from, includePage = false } if (!posts || posts.length === 0) { console.warn('文章列表为空') + const cacheKey = 'page_block_' + BLOG.NOTION_PAGE_ID + await delCacheData(cacheKey) } // Sort by date if (BLOG.POSTS_SORT_BY === 'date') { diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index bb91a655..61a5a934 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -59,7 +59,7 @@ export async function getGlobalNotionData ({ */ export async function getNotionPageData ({ pageId, from }) { // 尝试从缓存获取 - const cacheKey = 'page_record_map_' + pageId + const cacheKey = 'page_block_' + pageId const data = await getDataFromCache(cacheKey) if (data) { console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`) diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index ecf42217..b2ef8f19 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -6,7 +6,7 @@ export async function getPostBlocks (id, from, slice) { const cacheKey = 'page_block_' + id let pageBlock = await getDataFromCache(cacheKey) if (pageBlock) { - console.log('[请求缓存]:', `from:${from}`, `id:${id}`) + console.log('[请求缓存]:', `from:${from}`, `id:${id}`, cacheKey) return filterPostBlocks(id, pageBlock, slice) } const authToken = BLOG.NOTION_ACCESS_TOKEN || null