Files
NotionNext/lib/cache/cache_manager.js
2021-12-17 13:49:17 +08:00

48 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import BLOG from '@/blog.config'
import { getCacheFromFile, setCacheToFile } from '@/lib/cache/local_file_cache'
import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache'
const enableCache = true && !BLOG.isProd // 生产环境禁用
const cacheProvider = 'memory' // ['memory','file'] 用内存或data.json做缓存
/**
* 为减少频繁接口请求notion数据将被缓存
* @param {*} key
* @returns
*/
export async function getDataFromCache (key) {
if (!enableCache) {
return null
}
let dataFromCache
switch (cacheProvider) {
case 'memory':
dataFromCache = await getCacheFromMemory(key)
break
case 'file':
dataFromCache = await getCacheFromFile(key)
break
default:
break
}
if (JSON.stringify(dataFromCache) === '[]') {
return null
}
return dataFromCache
}
export async function setDataToCache (key, data) {
if (!enableCache || !data) {
return
}
switch (cacheProvider) {
case 'memory':
await setCacheToMemory(key, data)
break
case 'file':
await setCacheToFile(key, data)
break
default:
break
}
}