缓存优化
This commit is contained in:
tangly1024
2021-12-16 22:01:59 +08:00
parent 54026de51b
commit 6dc1794bdf
6 changed files with 80 additions and 27 deletions

View File

@@ -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
}
}