mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-18 07:26:48 +00:00
feature:
缓存优化
This commit is contained in:
35
lib/cache/cache_manager.js
vendored
35
lib/cache/cache_manager.js
vendored
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user