Files
warmbly/internal/app/user/cache.go
T
Matthew MeszarosandDevin AI acb5b8f9f4 fix: skip the onboarding wizard for seeded, sandbox, bootstrap, and warmblyctl-created accounts
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-04 17:49:23 +00:00

58 lines
1.3 KiB
Go

package user
import (
"context"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"github.com/warmbly/warmbly/internal/errx"
"github.com/warmbly/warmbly/internal/models"
"github.com/warmbly/warmbly/internal/observability/errs"
)
func getUserKey(id uuid.UUID) string {
return "user:" + id.String()
}
// CacheKey is the Redis key the cached /auth/me copy of a user lives under.
func CacheKey(id uuid.UUID) string {
return getUserKey(id)
}
func (s *userService) SaveUser(ctx context.Context, user *models.User) *errx.Error {
raw, err := json.Marshal(user)
if err != nil {
errs.CaptureException(err)
return errx.InternalError()
}
key := getUserKey(user.ID)
if err := s.cache.SetEx(ctx, key, raw, UserTTL).Err(); err != nil {
errs.CaptureException(err)
return errx.InternalError()
}
return nil
}
func (s *userService) getUser(ctx context.Context, userID uuid.UUID) (*models.User, *errx.Error) {
data, err := s.cache.Get(ctx, getUserKey(userID)).Bytes()
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, nil
}
errs.CaptureException(err)
return nil, errx.InternalError()
}
var user models.User
if err := json.Unmarshal(data, &user); err != nil {
errs.CaptureException(err)
return nil, errx.InternalError()
}
return &user, nil
}