Skip to content

Commit

Permalink
feat(redis 缓存时间转移至配置文件中):
Browse files Browse the repository at this point in the history
  • Loading branch information
qixing-jk committed Jan 11, 2025
1 parent fb35c44 commit 19cef99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
5 changes: 5 additions & 0 deletions conf/dev.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* 开发人员可能需要关注的配置
*/
const { NEXT_REVALIDATE_SECOND } = require('../blog.config')

module.exports = {
SUB_PATH: '', // leave this empty unless you want to deploy in a folder
DEBUG: process.env.NEXT_PUBLIC_DEBUG || false, // 是否显示调试按钮
Expand All @@ -10,6 +12,9 @@ module.exports = {

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

ENABLE_CACHE:
process.env.ENABLE_CACHE ||
Expand Down
7 changes: 1 addition & 6 deletions lib/cache/redis_cache.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import BLOG from '@/blog.config'
import { siteConfig } from '@/lib/config'
import Redis from 'ioredis'

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

export const redisCacheTime = Math.trunc(
siteConfig('NEXT_REVALIDATE_SECOND', BLOG.NEXT_REVALIDATE_SECOND) * 1.5
)

export async function getCache(key) {
try {
const data = await redisClient.get(key)
Expand All @@ -23,7 +18,7 @@ export async function setCache(key, data, customCacheTime) {
key,
JSON.stringify(data),
'EX',
customCacheTime || redisCacheTime
customCacheTime || BLOG.REDIS_CACHE_TIME
)
} catch (e) {
console.error('redisClient写入失败 ' + e)
Expand Down

0 comments on commit 19cef99

Please sign in to comment.