Skip to content

feat(设置Vercel生产环境下不使用的缓存类型): 针对特性缓存类型进行设置,不影响其他缓存方式,便于扩展 #3124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conf/dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lib/cache/cache_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 19 additions & 4 deletions lib/cache/local_file_cache.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import fs from 'fs'
import { isNotVercelProduction } from '@/lib/cache/cache_manager'

const path = require('path')
// 文件缓存持续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)
Expand All @@ -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
Expand All @@ -33,15 +39,21 @@ export async function getCache (key) {
* @param data
* @returns {Promise<null>}
*/
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
json[key + '_expire_time'] = new Date().getTime()
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
Expand All @@ -53,6 +65,9 @@ export async function delCache (key) {
* 清理缓存
*/
export async function cleanCache() {
if (!isNotVercelProduction) {
return
}
const json = {}
fs.writeFileSync(jsonFile, JSON.stringify(json))
}
Expand Down
10 changes: 10 additions & 0 deletions lib/cache/memory_cache.js
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/cache/redis_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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)
Expand Down
Loading