mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
feat(支持更加简单的的缓存数据读取和写入方式): 尝试从缓存中获取数据,如果没有则尝试获取数据并写入缓存,最终返回所需数据
This commit is contained in:
34
lib/cache/cache_manager.js
vendored
34
lib/cache/cache_manager.js
vendored
@@ -2,6 +2,40 @@ import BLOG from '@/blog.config'
|
||||
import FileCache from './local_file_cache'
|
||||
import MemoryCache from './memory_cache'
|
||||
|
||||
/**
|
||||
* 尝试从缓存中获取数据,如果没有则尝试获取数据并写入缓存,最终返回所需数据
|
||||
* @param key
|
||||
* @param getDataFunction
|
||||
* @param getDataArgs
|
||||
* @returns {Promise<*|null>}
|
||||
*/
|
||||
export async function getOrSetDataWithCache(key, getDataFunction, ...getDataArgs) {
|
||||
return getOrSetDataWithCustomCache(key, null, getDataFunction, ...getDataArgs)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 尝试从缓存中获取数据,如果没有则尝试获取数据并自定义写入缓存,最终返回所需数据
|
||||
* @param key
|
||||
* @param customCacheTime
|
||||
* @param getDataFunction
|
||||
* @param getDataArgs
|
||||
* @returns {Promise<*|null>}
|
||||
*/
|
||||
export async function getOrSetDataWithCustomCache(key, customCacheTime, getDataFunction, ...getDataArgs) {
|
||||
const dataFromCache = await getDataFromCache(key)
|
||||
if (dataFromCache) {
|
||||
console.log('[缓存-->>API]:', key)
|
||||
return dataFromCache
|
||||
}
|
||||
const data = await getDataFunction(...getDataArgs)
|
||||
if (data) {
|
||||
console.log('[API-->>缓存]:', key)
|
||||
await setDataToCache(key, data, customCacheTime)
|
||||
}
|
||||
return data || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 为减少频繁接口请求,notion数据将被缓存
|
||||
* @param {*} key
|
||||
|
||||
Reference in New Issue
Block a user