perf(session): ttl

This commit is contained in:
zhangheng
2026-04-23 14:47:14 +08:00
parent cd1bad6da9
commit 5b38f684c3
6 changed files with 488 additions and 222 deletions

View File

@@ -0,0 +1,34 @@
export const SESSION_COOKIE_NAME = "session"
const SESSION_KEY_PREFIX = "label:session:"
const normalizeClientIp = (ip: string) => ip.replace(/^::ffff:/, "")
export const createSessionRedisKey = () =>
`${SESSION_KEY_PREFIX}${crypto.randomUUID()}`
export const getSessionRedisKeyFromCookieValue = (
sessionCookieValue?: string | null
) => {
if (!sessionCookieValue) {
return ""
}
try {
const parsedValue = JSON.parse(sessionCookieValue)
return typeof parsedValue === "string" ? parsedValue : ""
} catch {
return sessionCookieValue
}
}
export const getClientIpFromRequest = (req: any) => {
const forwardedFor =
req?.headers?.get?.("x-forwarded-for") ||
req?.headers?.["x-forwarded-for"] ||
""
const clientIP =
forwardedFor?.split(",")[0]?.trim() || req?.socket?.remoteAddress || ""
return normalizeClientIp(clientIP)
}