Skip to content

Commit 19cef99

Browse files
committed
feat(redis 缓存时间转移至配置文件中):
1 parent fb35c44 commit 19cef99

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

conf/dev.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* 开发人员可能需要关注的配置
33
*/
4+
const { NEXT_REVALIDATE_SECOND } = require('../blog.config')
5+
46
module.exports = {
57
SUB_PATH: '', // leave this empty unless you want to deploy in a folder
68
DEBUG: process.env.NEXT_PUBLIC_DEBUG || false, // 是否显示调试按钮
@@ -10,6 +12,9 @@ module.exports = {
1012

1113
// Redis 缓存数据库地址 (警告:缓存时间使用了NEXT_REVALIDATE_SECOND,且无法从Notion获取)
1214
REDIS_URL: process.env.REDIS_URL || '',
15+
// Redis 缓存时间
16+
REDIS_CACHE_TIME:
17+
process.env.REDIS_CACHE_TIME || Math.trunc(NEXT_REVALIDATE_SECOND * 1.5),
1318

1419
ENABLE_CACHE:
1520
process.env.ENABLE_CACHE ||

lib/cache/redis_cache.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import BLOG from '@/blog.config'
2-
import { siteConfig } from '@/lib/config'
32
import Redis from 'ioredis'
43

54
export const redisClient = BLOG.REDIS_URL ? new Redis(BLOG.REDIS_URL) : {}
65

7-
export const redisCacheTime = Math.trunc(
8-
siteConfig('NEXT_REVALIDATE_SECOND', BLOG.NEXT_REVALIDATE_SECOND) * 1.5
9-
)
10-
116
export async function getCache(key) {
127
try {
138
const data = await redisClient.get(key)
@@ -23,7 +18,7 @@ export async function setCache(key, data, customCacheTime) {
2318
key,
2419
JSON.stringify(data),
2520
'EX',
26-
customCacheTime || redisCacheTime
21+
customCacheTime || BLOG.REDIS_CACHE_TIME
2722
)
2823
} catch (e) {
2924
console.error('redisClient写入失败 ' + e)

0 commit comments

Comments
 (0)