forked from mtusi2026/mtusi2026lab3
feat(auth): make session cache optional via SESSION_STORE
This commit is contained in:
+56
-25
@@ -93,18 +93,8 @@ func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID
|
||||
tokenHash := credentials.HashToken(token)
|
||||
key := cacheKey(tokenHash)
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
val, err := s.cache.Get(cctx, key).Result()
|
||||
cancel()
|
||||
|
||||
if err == nil {
|
||||
id, perr := uuid.Parse(val)
|
||||
if perr == nil {
|
||||
return id, nil
|
||||
}
|
||||
s.log.Warn("session cache value is not a uuid", "error", perr)
|
||||
} else if !errors.Is(err, goredis.Nil) {
|
||||
s.log.Warn("session cache get failed, falling back to database", "error", err)
|
||||
if id, ok := s.cacheGet(ctx, key); ok {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
row, err := s.q.GetActiveSession(ctx, tokenHash)
|
||||
@@ -115,14 +105,7 @@ func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
ttl := min(time.Until(row.ExpiresAt), sessionCacheTTL)
|
||||
if ttl > 0 {
|
||||
cctx, cancel = context.WithTimeout(ctx, cacheOpTimeout)
|
||||
if err := s.cache.Set(cctx, key, row.UserID.String(), ttl).Err(); err != nil {
|
||||
s.log.Warn("session cache set failed", "error", err)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
s.cacheSet(ctx, key, row.UserID, min(time.Until(row.ExpiresAt), sessionCacheTTL))
|
||||
|
||||
return row.UserID, nil
|
||||
}
|
||||
@@ -135,11 +118,7 @@ func (s *AuthService) Logout(ctx context.Context, token string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
if err := s.cache.Del(cctx, cacheKey(tokenHash)).Err(); err != nil {
|
||||
s.log.Warn("session cache delete failed", "error", err)
|
||||
}
|
||||
s.cacheDel(ctx, cacheKey(tokenHash))
|
||||
|
||||
if n == 0 {
|
||||
return ErrUnauthorized
|
||||
@@ -150,3 +129,55 @@ func (s *AuthService) Logout(ctx context.Context, token string) error {
|
||||
func cacheKey(tokenHash string) string {
|
||||
return "session:" + tokenHash
|
||||
}
|
||||
|
||||
func (s *AuthService) cacheGet(ctx context.Context, key string) (uuid.UUID, bool) {
|
||||
if s.cache == nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
|
||||
val, err := s.cache.Get(cctx, key).Result()
|
||||
switch {
|
||||
case err == nil:
|
||||
id, perr := uuid.Parse(val)
|
||||
if perr != nil {
|
||||
s.log.Warn("session cache value is not a uuid", "error", perr)
|
||||
return uuid.Nil, false
|
||||
}
|
||||
s.log.Debug("session cache hit")
|
||||
return id, true
|
||||
case errors.Is(err, goredis.Nil):
|
||||
s.log.Debug("session cache miss")
|
||||
default:
|
||||
s.log.Warn("session cache get failed, falling back to database", "error", err)
|
||||
}
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
func (s *AuthService) cacheSet(ctx context.Context, key string, id uuid.UUID, ttl time.Duration) {
|
||||
if s.cache == nil || ttl <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := s.cache.Set(cctx, key, id.String(), ttl).Err(); err != nil {
|
||||
s.log.Warn("session cache set failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AuthService) cacheDel(ctx context.Context, key string) {
|
||||
if s.cache == nil {
|
||||
return
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := s.cache.Del(cctx, key).Err(); err != nil {
|
||||
s.log.Warn("session cache delete failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user