Skip to content

Commit 4938d04

Browse files
committed
fix(cache): store user cache by both ID and UID
Because - getUserFromCacheByUID looks up cache with key user:{uid} - But setUserToCache stores cache with key user:{id} - These keys never match, causing cache to always miss This commit - Add setUserToCacheWithUID function that stores by both ID and UID keys - Update GetUserByUIDAdmin to use setUserToCacheWithUID
1 parent 6058c8c commit 4938d04

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pkg/service/cache.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ func (s *service) setUserToCache(ctx context.Context, user *mgmtpb.User) error {
7575
return s.setToCache(ctx, CacheTargetUser, user)
7676
}
7777

78+
func (s *service) setUserToCacheWithUID(ctx context.Context, user *mgmtpb.User, uid uuid.UUID) error {
79+
b, err := protojson.Marshal(user)
80+
if err != nil {
81+
return err
82+
}
83+
84+
// Cache by ID
85+
if err := s.redisClient.Set(ctx, fmt.Sprintf("%s:%s", CacheTargetUser, user.Id), b, 5*time.Minute).Err(); err != nil {
86+
return err
87+
}
88+
89+
// Also cache by UID for lookups by UID
90+
if err := s.redisClient.Set(ctx, fmt.Sprintf("%s:%s", CacheTargetUser, uid.String()), b, 5*time.Minute).Err(); err != nil {
91+
return err
92+
}
93+
94+
return nil
95+
}
96+
7897
func (s *service) deleteFromCacheByID(ctx context.Context, target string, id string) error {
7998
// Delete only by ID (UID is no longer in the protobuf)
8099
setCmd := s.redisClient.Del(ctx, fmt.Sprintf("%s:%s", target, id))

pkg/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (s *service) GetUserByUIDAdmin(ctx context.Context, uid uuid.UUID) (*mgmtpb
232232
if err != nil {
233233
return nil, err
234234
}
235-
err = s.setUserToCache(ctx, pbUser)
235+
err = s.setUserToCacheWithUID(ctx, pbUser, uid)
236236
if err != nil {
237237
return nil, err
238238
}

0 commit comments

Comments
 (0)