mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
34
lib/cache/cache_manager.js
vendored
34
lib/cache/cache_manager.js
vendored
@@ -2,6 +2,40 @@ import BLOG from '@/blog.config'
|
||||
import FileCache from './local_file_cache'
|
||||
import MemoryCache from './memory_cache'
|
||||
|
||||
/**
|
||||
* 尝试从缓存中获取数据,如果没有则尝试获取数据并写入缓存,最终返回所需数据
|
||||
* @param key
|
||||
* @param getDataFunction
|
||||
* @param getDataArgs
|
||||
* @returns {Promise<*|null>}
|
||||
*/
|
||||
export async function getOrSetDataWithCache(key, getDataFunction, ...getDataArgs) {
|
||||
return getOrSetDataWithCustomCache(key, null, getDataFunction, ...getDataArgs)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 尝试从缓存中获取数据,如果没有则尝试获取数据并自定义写入缓存,最终返回所需数据
|
||||
* @param key
|
||||
* @param customCacheTime
|
||||
* @param getDataFunction
|
||||
* @param getDataArgs
|
||||
* @returns {Promise<*|null>}
|
||||
*/
|
||||
export async function getOrSetDataWithCustomCache(key, customCacheTime, getDataFunction, ...getDataArgs) {
|
||||
const dataFromCache = await getDataFromCache(key)
|
||||
if (dataFromCache) {
|
||||
console.log('[缓存-->>API]:', key)
|
||||
return dataFromCache
|
||||
}
|
||||
const data = await getDataFunction(...getDataArgs)
|
||||
if (data) {
|
||||
console.log('[API-->>缓存]:', key)
|
||||
await setDataToCache(key, data, customCacheTime)
|
||||
}
|
||||
return data || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 为减少频繁接口请求,notion数据将被缓存
|
||||
* @param {*} key
|
||||
|
||||
@@ -12,6 +12,7 @@ import { deepClone } from '@/lib/utils'
|
||||
import { idToUuid } from 'notion-utils'
|
||||
import { siteConfig } from '../config'
|
||||
import { extractLangId, extractLangPrefix, getShortId } from '../utils/pageId'
|
||||
import { getOrSetDataWithCache } from '@/lib/cache/cache_manager'
|
||||
|
||||
export { getAllTags } from '../notion/getAllTags'
|
||||
export { getPost } from '../notion/getNotionPost'
|
||||
@@ -65,14 +66,15 @@ export async function getGlobalData({
|
||||
*/
|
||||
export async function getSiteDataByPageId({ pageId, from }) {
|
||||
// 获取NOTION原始数据,此接支持mem缓存。
|
||||
const pageRecordMap = await getPage(pageId, from)
|
||||
// 将Notion数据按规则转成站点数据
|
||||
const data = await converNotionToSiteDate(
|
||||
return await getOrSetDataWithCache(
|
||||
`site_data_${pageId}`,
|
||||
async (pageId, from) => {
|
||||
const pageRecordMap = await getPage(pageId, from)
|
||||
return convertNotionToSiteDate(pageId, from, deepClone(pageRecordMap))
|
||||
},
|
||||
pageId,
|
||||
from,
|
||||
deepClone(pageRecordMap)
|
||||
from
|
||||
)
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +141,7 @@ const EmptyData = pageId => {
|
||||
* 这里统一对数据格式化
|
||||
* @returns {Promise<JSX.Element|null|*>}
|
||||
*/
|
||||
async function converNotionToSiteDate(pageId, from, pageRecordMap) {
|
||||
async function convertNotionToSiteDate(pageId, from, pageRecordMap) {
|
||||
if (!pageRecordMap) {
|
||||
console.error('can`t get Notion Data ; Which id is: ', pageId)
|
||||
return {}
|
||||
@@ -273,11 +275,12 @@ async function converNotionToSiteDate(pageId, from, pageRecordMap) {
|
||||
categoryOptions: getCategoryOptions(schema)
|
||||
})
|
||||
// 所有标签
|
||||
const tagOptions = getAllTags({
|
||||
allPages,
|
||||
tagOptions: getTagOptions(schema),
|
||||
NOTION_CONFIG
|
||||
})
|
||||
const tagOptions =
|
||||
getAllTags({
|
||||
allPages,
|
||||
tagOptions: getTagOptions(schema),
|
||||
NOTION_CONFIG
|
||||
}) || null
|
||||
// 旧的菜单
|
||||
const customNav = getCustomNav({
|
||||
allPages: collectionData.filter(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||
import { getDataFromCache, getOrSetDataWithCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||
import { deepClone, delay } from '../utils'
|
||||
import notionAPI from '@/lib/notion/getNotionAPI'
|
||||
|
||||
@@ -11,21 +11,28 @@ import notionAPI 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 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 convertNotionBlocksToPost(id, pageBlock, slice)
|
||||
}
|
||||
return pageBlock
|
||||
if (pageBlock) {
|
||||
await setDataToCache(cacheKey, pageBlock)
|
||||
return convertNotionBlocksToPost(id, pageBlock, slice)
|
||||
}
|
||||
return pageBlock
|
||||
},
|
||||
id,
|
||||
slice
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user