Skip to content

Commit 57410c7

Browse files
authored
Merge pull request #25 from nixopus/perf/redis-session-cache
perf: add Redis secondary storage and cookie cache for sessions
2 parents 23afcc4 + 7978fb3 commit 57410c7

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

bun.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
"@better-auth/api-key": "^1.5.5",
2626
"@better-auth/oauth-provider": "^1.5.5",
2727
"@better-auth/passkey": "^1.5.5",
28+
"@better-auth/redis-storage": "^1.5.6",
2829
"@dodopayments/better-auth": "^1.4.3",
2930
"better-auth": "^1.5.5",
3031
"dodopayments": "^2.17.2",
3132
"drizzle-orm": "^0.45.1",
3233
"hono": "^4.0.0",
34+
"ioredis": "^5.10.1",
3335
"pg": "^8.17.1",
3436
"pino": "^10.3.1",
3537
"resend": "^6.7.0",

src/auth/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { defaultRoles } from 'better-auth/plugins/organization/access';
55
import { apiKey } from '@better-auth/api-key';
66
import { passkey } from '@better-auth/passkey';
77
import { oauthProvider } from '@better-auth/oauth-provider';
8+
import { redisStorage } from '@better-auth/redis-storage';
9+
import { Redis } from 'ioredis';
810
import {
911
dodopayments,
1012
checkout,
@@ -33,6 +35,7 @@ export const auth = betterAuth({
3335
provider: 'pg',
3436
schema,
3537
}),
38+
...(config.redisUrl ? { secondaryStorage: redisStorage({ client: new Redis(config.redisUrl) }) } : {}),
3639
baseURL: config.corsAllowedOrigins[0] || config.betterAuthUrl,
3740
basePath: '/api/auth',
3841
secret: config.betterAuthSecret,
@@ -191,6 +194,11 @@ export const auth = betterAuth({
191194
expiresIn: 60 * 60 * 24 * 7,
192195
updateAge: 60 * 60 * 24,
193196
storeSessionInDatabase: true,
197+
cookieCache: {
198+
enabled: true,
199+
maxAge: 5 * 60,
200+
strategy: 'compact',
201+
},
194202
},
195203
hooks: {
196204
before: beforeHook,

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export const config = {
3636
// Required for production, should be set via DATABASE_URL environment variable
3737
databaseUrl: process.env.DATABASE_URL || '',
3838

39+
// Redis URL for session caching (secondary storage)
40+
redisUrl: process.env.REDIS_URL || '',
41+
3942
// Auth service configuration
4043
betterAuthUrl: process.env.AUTH_SERVICE_URL || `http://localhost:${parseInt(process.env.PORT || '8080', 10)}`,
4144
betterAuthSecret: process.env.AUTH_SERVICE_SECRET || 'better-auth-secret-change-in-production',

0 commit comments

Comments
 (0)