Skip to content

Commit

Permalink
feat(优先使用 upstash Redis 进行 UUID 重定向):
Browse files Browse the repository at this point in the history
  • Loading branch information
qixing-jk committed Jan 11, 2025
1 parent ca50424 commit a3ebccf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
3 changes: 2 additions & 1 deletion blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const BLOG = {
'Hi,我是一个程序员, Hi,我是一个打工人,Hi,我是一个干饭人,欢迎来到我的博客🎉',

// uuid重定向至 slug(不支持Notion配置!)
UUID_REDIRECT: process.env.UUID_REDIRECT || false
UUID_REDIRECT: process.env.UUID_REDIRECT || false,
REDIRECT_CACHE_KEY: process.env.REDIRECT_CACHE_KEY || 'uuid_slug_map'
}

module.exports = BLOG
25 changes: 20 additions & 5 deletions lib/redirect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'fs'
import { redisCacheTime, redisClient } from '@/lib/cache/redis_cache'

export const redirectCacheKey = 'uuid_slug_map'
import {
upstashRedisCacheTime,
upstashRedisClient
} from '@/lib/cache/upstash_redis_cache'
import BLOG from '@/blog.config'

export async function generateRedirectJson({ allPages }) {
let uuidSlugMap = {}
Expand All @@ -10,12 +13,24 @@ export async function generateRedirectJson({ allPages }) {
uuidSlugMap[page.id] = page.slug
}
})
if (redisClient) {
if (upstashRedisClient) {
try {
await upstashRedisClient.hset(BLOG.REDIRECT_CACHE_KEY, uuidSlugMap)

await upstashRedisClient.expire(
BLOG.REDIRECT_CACHE_KEY,
upstashRedisCacheTime
)
} catch (e) {
console.warn('写入 upstashRedis 失败', e)
}
} else if (redisClient) {
try {
await redisClient.hset(
redirectCacheKey,
BLOG.REDIRECT_CACHE_KEY,
uuidSlugMap,
async () => await redisClient.expire(redirectCacheKey, redisCacheTime)
async () =>
await redisClient.expire(BLOG.REDIRECT_CACHE_KEY, redisCacheTime)
)
} catch (e) {
console.warn('写入Redis失败', e)
Expand Down
17 changes: 11 additions & 6 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import {
checkStrIsNotionId,
checkStrIsUuid,
getLastPartOfUrl
} from '@/lib/utils'
import { checkStrIsNotionId, checkStrIsUuid, getLastPartOfUrl } from '@/lib/utils'
import { idToUuid } from 'notion-utils'
import BLOG from './blog.config'
import { upstashRedisClient } from '@/lib/cache/upstash_redis_cache'

/**
* Clerk 身份验证中间件
Expand Down Expand Up @@ -46,7 +43,15 @@ const noAuthMiddleware = async (req: NextRequest, ev: any) => {
}
if (checkStrIsUuid(lastPart)) {
let redirectJson: Record<string, string | null> = {}
if (BLOG.REDIS_URL) {
if (upstashRedisClient) {
const redisResult = (await upstashRedisClient.hget(
BLOG.REDIRECT_CACHE_KEY,
lastPart
)) as string
redirectJson = {
[lastPart]: redisResult
}
} else if (BLOG.REDIS_URL) {
try {
const redisResponse = await fetch(
`${req.nextUrl.origin}/api/redirect`,
Expand Down
5 changes: 3 additions & 2 deletions pages/api/redirect.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { redisCacheTime, redisClient } from '@/lib/cache/redis_cache'
import { redirectCacheKey } from '@/lib/redirect'
import BLOG from '@/blog.config'

export default async function handler(req, res) {
const { lastPart } = req.body
try {
const result = (await redisClient.hget(redirectCacheKey, lastPart)) || null
const result =
(await redisClient.hget(BLOG.REDIRECT_CACHE_KEY, lastPart)) || null
res.setHeader(
'Cache-Control',
`public, max-age=${redisCacheTime}, stale-while-revalidate=${redisCacheTime / 6}`
Expand Down

0 comments on commit a3ebccf

Please sign in to comment.