perf(使用缓存优化): 复杂的数据转换函数使用缓存优化

This commit is contained in:
anime
2025-01-02 20:45:18 +08:00
parent 2766cb8d3b
commit b563744e24
2 changed files with 26 additions and 34 deletions

View File

@@ -66,13 +66,14 @@ export async function getGlobalData({
*/
export async function getSiteDataByPageId({ pageId, from }) {
// 获取NOTION原始数据此接支持mem缓存。
const pageRecordMap = await getPage(pageId, from)
return await getOrSetDataWithCache(
`site_data_${pageId}`,
converNotionToSiteDate,
async (pageId, from) => {
const pageRecordMap = await getPage(pageId, from)
return converNotionToSiteDate(pageId, from, deepClone(pageRecordMap))
},
pageId,
from,
deepClone(pageRecordMap)
from
)
}

View File

@@ -1,9 +1,5 @@
import BLOG from '@/blog.config'
import {
getDataFromCache,
getOrSetDataWithCache,
setDataToCache
} from '@/lib/cache/cache_manager'
import { getDataFromCache, getOrSetDataWithCache, setDataToCache } from '@/lib/cache/cache_manager'
import { deepClone, delay } from '../utils'
import getNotionAPI from '@/lib/notion/getNotionAPI'
@@ -15,33 +11,28 @@ import getNotionAPI from '@/lib/notion/getNotionAPI'
* @returns
*/
export async function getPage(id, from = null, slice) {
const cacheKey = `page_block_${id}`
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
// console.debug('[API<<--缓存]', `from:${from}`, cacheKey)
return await getOrSetDataWithCache(
`page_converted_block_${id}_${slice}`,
convertNotionBlocksToPost,
id,
pageBlock,
slice
)
}
return await getOrSetDataWithCache(
`page_content_${id}_${slice}`,
async (id, slice) => {
const cacheKey = `page_block_${id}`
let pageBlock = await getDataFromCache(cacheKey)
if (pageBlock) {
// console.debug('[API<<--缓存]', `from:${from}`, cacheKey)
return convertNotionBlocksToPost(id, pageBlock, slice)
}
// 抓取最新数据
pageBlock = await getPageWithRetry(id, from)
// 抓取最新数据
pageBlock = await getPageWithRetry(id, from)
if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
return await getOrSetDataWithCache(
`page_converted_block_${id}_${slice}`,
convertNotionBlocksToPost,
id,
pageBlock,
slice
)
}
return pageBlock
if (pageBlock) {
await setDataToCache(cacheKey, pageBlock)
return convertNotionBlocksToPost(id, pageBlock, slice)
}
return pageBlock
},
id,
slice
)
}
/**