mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-07 23:16:52 +00:00
缓存优化
This commit is contained in:
11
lib/cache/cache_manager.js
vendored
11
lib/cache/cache_manager.js
vendored
@@ -1,5 +1,5 @@
|
|||||||
import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache'
|
import { getCacheFromMemory, setCacheToMemory, delCacheFromMemory } from '@/lib/cache/memory_cache'
|
||||||
// import { getCacheFromFile, setCacheToFile } from './local_file_cache'
|
// import { getCacheFromFile, setCacheToFile, delCacheFromFile } from './local_file_cache'
|
||||||
const enableCache = true // 生产环境禁用
|
const enableCache = true // 生产环境禁用
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,3 +24,10 @@ export async function setDataToCache (key, data) {
|
|||||||
}
|
}
|
||||||
await setCacheToMemory(key, data)
|
await setCacheToMemory(key, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function delCacheData (key) {
|
||||||
|
if (!enableCache) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await delCacheFromMemory(key)
|
||||||
|
}
|
||||||
|
|||||||
8
lib/cache/local_file_cache.js
vendored
8
lib/cache/local_file_cache.js
vendored
@@ -40,3 +40,11 @@ export async function setCacheToFile (key, data) {
|
|||||||
json[key + '_expire_time'] = new Date().getTime()
|
json[key + '_expire_time'] = new Date().getTime()
|
||||||
fs.writeFileSync(jsonFile, JSON.stringify(json))
|
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))
|
||||||
|
}
|
||||||
|
|||||||
4
lib/cache/memory_cache.js
vendored
4
lib/cache/memory_cache.js
vendored
@@ -10,3 +10,7 @@ export async function getCacheFromMemory (key, options) {
|
|||||||
export async function setCacheToMemory (key, data) {
|
export async function setCacheToMemory (key, data) {
|
||||||
await cache.put(key, data, cacheTime * 1000)
|
await cache.put(key, data, cacheTime * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function delCacheFromMemory (key) {
|
||||||
|
await cache.del(key)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import getAllPageIds from './getAllPageIds'
|
|||||||
import getPageProperties from './getPageProperties'
|
import getPageProperties from './getPageProperties'
|
||||||
import { defaultMapImageUrl } from 'react-notion-x'
|
import { defaultMapImageUrl } from 'react-notion-x'
|
||||||
import { getNotionPageData } from '@/lib/notion/getNotionData'
|
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) {
|
if (!posts || posts.length === 0) {
|
||||||
console.warn('文章列表为空')
|
console.warn('文章列表为空')
|
||||||
|
const cacheKey = 'page_block_' + BLOG.NOTION_PAGE_ID
|
||||||
|
await delCacheData(cacheKey)
|
||||||
}
|
}
|
||||||
// Sort by date
|
// Sort by date
|
||||||
if (BLOG.POSTS_SORT_BY === 'date') {
|
if (BLOG.POSTS_SORT_BY === 'date') {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export async function getGlobalNotionData ({
|
|||||||
*/
|
*/
|
||||||
export async function getNotionPageData ({ pageId, from }) {
|
export async function getNotionPageData ({ pageId, from }) {
|
||||||
// 尝试从缓存获取
|
// 尝试从缓存获取
|
||||||
const cacheKey = 'page_record_map_' + pageId
|
const cacheKey = 'page_block_' + pageId
|
||||||
const data = await getDataFromCache(cacheKey)
|
const data = await getDataFromCache(cacheKey)
|
||||||
if (data) {
|
if (data) {
|
||||||
console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`)
|
console.log('[请求缓存]:', `from:${from}`, `id:${pageId}`)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export async function getPostBlocks (id, from, slice) {
|
|||||||
const cacheKey = 'page_block_' + id
|
const cacheKey = 'page_block_' + id
|
||||||
let pageBlock = await getDataFromCache(cacheKey)
|
let pageBlock = await getDataFromCache(cacheKey)
|
||||||
if (pageBlock) {
|
if (pageBlock) {
|
||||||
console.log('[请求缓存]:', `from:${from}`, `id:${id}`)
|
console.log('[请求缓存]:', `from:${from}`, `id:${id}`, cacheKey)
|
||||||
return filterPostBlocks(id, pageBlock, slice)
|
return filterPostBlocks(id, pageBlock, slice)
|
||||||
}
|
}
|
||||||
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
|
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
|
||||||
|
|||||||
Reference in New Issue
Block a user