mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
feat(初步完成Redis缓存功能):
This commit is contained in:
5
lib/cache/cache_manager.js
vendored
5
lib/cache/cache_manager.js
vendored
@@ -1,6 +1,7 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import FileCache from './local_file_cache'
|
||||
import MemoryCache from './memory_cache'
|
||||
import RedisCache from './redis_cache'
|
||||
|
||||
/**
|
||||
* 为减少频繁接口请求,notion数据将被缓存
|
||||
@@ -40,7 +41,9 @@ export async function delCacheData(key) {
|
||||
* @returns
|
||||
*/
|
||||
function getApi() {
|
||||
if (process.env.ENABLE_FILE_CACHE) {
|
||||
if (process.env.REDIS_URL) {
|
||||
return RedisCache
|
||||
} else if (process.env.ENABLE_FILE_CACHE) {
|
||||
return FileCache
|
||||
} else {
|
||||
return MemoryCache
|
||||
|
||||
40
lib/cache/redis_cache.js
vendored
Normal file
40
lib/cache/redis_cache.js
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
import Redis from 'ioredis'
|
||||
import BLOG from '@/blog.config'
|
||||
import { siteConfig } from '@/lib/config'
|
||||
|
||||
const redisClient = new Redis(process.env.REDIS_URL)
|
||||
const cacheTime = Math.trunc(
|
||||
siteConfig('NEXT_REVALIDATE_SECOND', BLOG.NEXT_REVALIDATE_SECOND) * 1.5
|
||||
)
|
||||
|
||||
export async function getCache(key) {
|
||||
try {
|
||||
const data = await redisClient.get(key)
|
||||
return data ? JSON.parse(data) : null
|
||||
} catch (e) {
|
||||
console.error('redisClient读取失败 ' + e)
|
||||
}
|
||||
}
|
||||
|
||||
export async function setCache(key, data, customCacheTime) {
|
||||
try {
|
||||
await redisClient.set(
|
||||
key,
|
||||
JSON.stringify(data),
|
||||
'EX',
|
||||
customCacheTime || cacheTime
|
||||
)
|
||||
} catch (e) {
|
||||
console.error('redisClient写入失败 ' + e)
|
||||
}
|
||||
}
|
||||
|
||||
export async function delCache(key) {
|
||||
try {
|
||||
await redisClient.del(key)
|
||||
} catch (e) {
|
||||
console.error('redisClient删除失败 ' + e)
|
||||
}
|
||||
}
|
||||
|
||||
export default { getCache, setCache, delCache }
|
||||
Reference in New Issue
Block a user