From 6dc1794bdff01c954b7597234df759b3cb538de1 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Thu, 16 Dec 2021 22:01:59 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=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 | 35 +++++++++++++++--------- lib/cache/memory_cache.js | 2 +- lib/notion/getNotionData.js | 5 ++-- lib/notion/getPostBlocks.js | 5 ++-- pages/article/[slug].js | 6 ++--- public/sitemap.xml | 54 ++++++++++++++++++++++++++++++++----- 6 files changed, 80 insertions(+), 27 deletions(-) diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index b28c2c0f..8d0dd56c 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -1,8 +1,8 @@ +import BLOG from '@/blog.config' import { getCacheFromFile, setCacheToFile } from '@/lib/cache/local_file_cache' import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache' -import BLOG from '@/blog.config' -// 关闭本地缓存 -const enableCache = true +const enableCache = true && !BLOG.isProd // 生产环境禁用 +const cacheProvider = 'memory' // ['memory','file'] 用内存或data.json做缓存 /** * 为减少频繁接口请求,notion数据将被缓存 @@ -14,21 +14,32 @@ export async function getDataFromCache (key) { return null } let dataFromCache - if (BLOG.isProd) { - dataFromCache = await getCacheFromMemory(key) - } else { - dataFromCache = await getCacheFromFile(key) + switch (cacheProvider) { + case 'memory': + dataFromCache = await getCacheFromMemory(key) + break + case 'file': + dataFromCache = await getCacheFromFile(key) + break + default: + break } + return dataFromCache } export async function setDataToCache (key, data) { - if (!enableCache) { + if (!enableCache || !data) { return } - if (BLOG.isProd) { - await setCacheToMemory(key, data) - } else { - await setCacheToFile(key, data) + switch (cacheProvider) { + case 'memory': + await setCacheToMemory(key, data) + break + case 'file': + await setCacheToFile(key, data) + break + default: + break } } diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index cc3e8bac..ea16cec5 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -5,5 +5,5 @@ export async function getCacheFromMemory (key, options) { // url为缓存标识 } export async function setCacheToMemory (key, data) { // url为缓存标识 - await cache.put(key, data, 60 * 1000) + await cache.put(key, data, 5 * 60 * 1000) } diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index d7701892..f65eee4b 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -11,7 +11,8 @@ import { getPostBlocks } from '@/lib/notion/getPostBlocks' */ export async function getNotionPageData ({ pageId = BLOG.notionPageId, from }) { // 尝试从缓存获取 - const data = await getDataFromCache('page_record_map_' + pageId) + const cacheKey = 'page_record_map_' + pageId + const data = await getDataFromCache(cacheKey) if (data) { console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`) return data @@ -19,7 +20,7 @@ export async function getNotionPageData ({ pageId = BLOG.notionPageId, from }) { const pageRecordMap = await getPageRecordMapByNotionAPI({ pageId, from }) // 存入缓存 if (pageRecordMap) { - await setDataToCache('page_record_map', pageRecordMap) + await setDataToCache(cacheKey, pageRecordMap) } return pageRecordMap } diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index ff037545..175c426b 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -3,7 +3,8 @@ import { NotionAPI } from 'notion-client' import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' export async function getPostBlocks (id, from) { - let pageBlock = await getDataFromCache('page_block_' + id) + const cacheKey = 'page_block_' + id + let pageBlock = await getDataFromCache(cacheKey) if (pageBlock) { console.log('[请求缓存]:', `from:${from}`, `id:${id}`) return pageBlock @@ -20,7 +21,7 @@ export async function getPostBlocks (id, from) { } if (pageBlock) { - await setDataToCache('page_block_' + id, pageBlock) + await setDataToCache(cacheKey, pageBlock) } return pageBlock } diff --git a/pages/article/[slug].js b/pages/article/[slug].js index 5a172879..d2ef039a 100644 --- a/pages/article/[slug].js +++ b/pages/article/[slug].js @@ -31,9 +31,9 @@ const Slug = ({ post, blockMap, tags, prev, next, allPosts, recommendPosts, cate export async function getStaticPaths () { let posts = [] - if (BLOG.isProd) { - posts = await getAllPosts({ from: 'slug - paths', includePage: true }) - } + // if (BLOG.isProd) { + posts = await getAllPosts({ from: 'slug - paths', includePage: true }) + // } return { paths: posts.map(row => `${BLOG.path}/article/${row.slug}`), fallback: true diff --git a/public/sitemap.xml b/public/sitemap.xml index cca7c276..1f551e90 100644 --- a/public/sitemap.xml +++ b/public/sitemap.xml @@ -1,10 +1,50 @@ -https://tangly1024.comdaily0.72021-12-16T03:34:52.290Z -https://tangly1024.com/aboutdaily0.72021-12-16T03:34:52.290Z -https://tangly1024.com/archivedaily0.72021-12-16T03:34:52.290Z -https://tangly1024.com/categorydaily0.72021-12-16T03:34:52.290Z -https://tangly1024.com/feeddaily0.72021-12-16T03:34:52.290Z -https://tangly1024.com/searchdaily0.72021-12-16T03:34:52.290Z -https://tangly1024.com/tagdaily0.72021-12-16T03:34:52.290Z +https://tangly1024.comdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tagdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/searchdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/aboutdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/archivedaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/categorydaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/category/技术分享daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/category/碎片杂文daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/category/投资理财daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/category/读书笔记daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Notiondaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Blogdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Verceldaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/NextJSdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Nobeliumdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Fruitiondaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Hexodaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/开发daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Serverlessdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Databasedaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/新闻daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/经济daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/工具daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/CVSdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Macdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/金钱daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/思考daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/VSCodedaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/NodeJSdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Parallels Desktopdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/虚拟机daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/市场daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/ReactJSdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/文字daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/Pythondaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/WebStormdaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/营销daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/健康daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/查理·芒格daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/企业daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/投资daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/教育daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/心理daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/indigodaily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/品牌daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/广告daily0.72021-12-16T13:36:37.657Z +https://tangly1024.com/tag/区块链daily0.72021-12-16T13:36:37.657Z \ No newline at end of file