Files
NotionNext/lib/cache/cache_manager.js
anime 148545cea9 feat(原生AI摘要功能支持缓存): 自定义缓存功能只支持memory,不支持文件缓存
(cherry picked from commit dd56dae44f7f555d9004e7d1f872085bded2cb86)
2024-12-29 00:21:37 +08:00

49 lines
1.0 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 FileCache from './local_file_cache'
import MemoryCache from './memory_cache'
/**
* 为减少频繁接口请求notion数据将被缓存
* @param {*} key
* @returns
*/
export async function getDataFromCache(key, force) {
if (BLOG.ENABLE_CACHE || force) {
const dataFromCache = await getApi().getCache(key)
if (!dataFromCache || JSON.stringify(dataFromCache) === '[]') {
return null
}
// console.trace('[API-->>缓存]:', key, dataFromCache)
return dataFromCache
} else {
return null
}
}
export async function setDataToCache(key, data, customCacheTime) {
if (!data) {
return
}
// console.trace('[API-->>缓存写入]:', key)
await getApi().setCache(key, data, customCacheTime)
}
export async function delCacheData(key) {
if (!JSON.parse(BLOG.ENABLE_CACHE)) {
return
}
await getApi().delCache(key)
}
/**
* 缓存实现类
* @returns
*/
function getApi() {
if (process.env.ENABLE_FILE_CACHE) {
return FileCache
} else {
return MemoryCache
}
}