diff --git a/conf/dev.config.js b/conf/dev.config.js index c34a8c6430a..6151ae5d8e6 100644 --- a/conf/dev.config.js +++ b/conf/dev.config.js @@ -8,7 +8,7 @@ module.exports = { BACKGROUND_LIGHT: '#eeeeee', // use hex value, don't forget '#' e.g #fffefc BACKGROUND_DARK: '#000000', // use hex value, don't forget '#' - // Redis 缓存数据库地址 + // Redis 缓存数据库地址 (警告:缓存时间使用了NEXT_REVALIDATE_SECOND,且无法从Notion获取) REDIS_URL: process.env.REDIS_URL || '', ENABLE_CACHE: diff --git a/lib/cache/cache_manager.js b/lib/cache/cache_manager.js index f0c412abd5f..ad9586a3b5f 100644 --- a/lib/cache/cache_manager.js +++ b/lib/cache/cache_manager.js @@ -4,7 +4,7 @@ import MemoryCache from './memory_cache' import RedisCache from './redis_cache' // 配置是否开启Vercel环境中的缓存,因为Vercel中现有两种缓存方式在无服务环境下基本都是无意义的,纯粹的浪费资源 -const enableCacheInVercel = +export const isNotVercelProduction = process.env.npm_lifecycle_event === 'build' || process.env.npm_lifecycle_event === 'export' || !BLOG['isProd'] @@ -77,7 +77,7 @@ export async function getDataFromCache(key, force) { * @returns */ export async function setDataToCache(key, data, customCacheTime) { - if (!enableCacheInVercel || !data) { + if (!data) { return } // console.trace('[API-->>缓存写入]:', key) diff --git a/lib/cache/local_file_cache.js b/lib/cache/local_file_cache.js index c411d6e59d2..6422db0e007 100644 --- a/lib/cache/local_file_cache.js +++ b/lib/cache/local_file_cache.js @@ -1,4 +1,5 @@ import fs from 'fs' +import { isNotVercelProduction } from '@/lib/cache/cache_manager' const path = require('path') // 文件缓存持续10秒 @@ -6,7 +7,10 @@ const cacheInvalidSeconds = 1000000000 * 1000 // 文件名 const jsonFile = path.resolve('./data.json') -export async function getCache (key) { +export async function getCache(key) { + if (!isNotVercelProduction) { + return + } const exist = await fs.existsSync(jsonFile) if (!exist) return null const data = await fs.readFileSync(jsonFile) @@ -19,7 +23,9 @@ export async function getCache (key) { return null } // 缓存超过有效期就作废 - const cacheValidTime = new Date(parseInt(json[key + '_expire_time']) + cacheInvalidSeconds) + const cacheValidTime = new Date( + parseInt(json[key + '_expire_time']) + cacheInvalidSeconds + ) const currentTime = new Date() if (!cacheValidTime || cacheValidTime < currentTime) { return null @@ -33,7 +39,10 @@ export async function getCache (key) { * @param data * @returns {Promise} */ -export async function setCache (key, data) { +export async function setCache(key, data) { + if (!isNotVercelProduction) { + return + } const exist = await fs.existsSync(jsonFile) const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {} json[key] = data @@ -41,7 +50,10 @@ export async function setCache (key, data) { fs.writeFileSync(jsonFile, JSON.stringify(json)) } -export async function delCache (key) { +export async function delCache(key) { + if (!isNotVercelProduction) { + return + } const exist = await fs.existsSync(jsonFile) const json = exist ? JSON.parse(await fs.readFileSync(jsonFile)) : {} delete json.key @@ -53,6 +65,9 @@ export async function delCache (key) { * 清理缓存 */ export async function cleanCache() { + if (!isNotVercelProduction) { + return + } const json = {} fs.writeFileSync(jsonFile, JSON.stringify(json)) } diff --git a/lib/cache/memory_cache.js b/lib/cache/memory_cache.js index 001666c3fe0..183353b50dd 100644 --- a/lib/cache/memory_cache.js +++ b/lib/cache/memory_cache.js @@ -1,17 +1,27 @@ import cache from 'memory-cache' import BLOG from 'blog.config' +import { isNotVercelProduction } from '@/lib/cache/cache_manager' const cacheTime = BLOG.isProd ? 10 * 60 : 120 * 60 // 120 minutes for dev,10 minutes for prod export async function getCache(key, options) { + if (!isNotVercelProduction) { + return + } return await cache.get(key) } export async function setCache(key, data, customCacheTime) { + if (!isNotVercelProduction) { + return + } await cache.put(key, data, (customCacheTime || cacheTime) * 1000) } export async function delCache(key) { + if (!isNotVercelProduction) { + return + } await cache.del(key) } diff --git a/lib/cache/redis_cache.js b/lib/cache/redis_cache.js index b35f472ad54..30d65a5f841 100644 --- a/lib/cache/redis_cache.js +++ b/lib/cache/redis_cache.js @@ -4,7 +4,7 @@ import Redis from 'ioredis' export const redisClient = BLOG.REDIS_URL ? new Redis(BLOG.REDIS_URL) : {} -const cacheTime = Math.trunc( +export const redisCacheTime = Math.trunc( siteConfig('NEXT_REVALIDATE_SECOND', BLOG.NEXT_REVALIDATE_SECOND) * 1.5 ) @@ -23,7 +23,7 @@ export async function setCache(key, data, customCacheTime) { key, JSON.stringify(data), 'EX', - customCacheTime || cacheTime + customCacheTime || redisCacheTime ) } catch (e) { console.error('redisClient写入失败 ' + e)