feat(auth): cache sessions in redis with postgres fallback

This commit is contained in:
kk10
2026-10-06 15:36:52 +03:00
parent f7e1866d4e
commit 5bd210b59d
7 changed files with 126 additions and 15 deletions
+31
View File
@@ -1 +1,32 @@
package redis
import (
"context"
"fmt"
"log/slog"
"time"
goredis "github.com/redis/go-redis/v9"
)
func New(ctx context.Context, log *slog.Logger, url string) (*goredis.Client, error) {
opts, err := goredis.ParseURL(url)
if err != nil {
return nil, fmt.Errorf("parse redis url: %w", err)
}
opts.DialTimeout = 1 * time.Second
opts.ReadTimeout = 200 * time.Millisecond
opts.WriteTimeout = 200 * time.Millisecond
opts.MaxRetries = -1
client := goredis.NewClient(opts)
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
if err := client.Ping(pingCtx).Err(); err != nil {
log.Warn("redis unavailable, running without cache", "error", err)
}
return client, nil
}