Skip to content

Commit 4fabdb5

Browse files
author
Jeff Yanta
committed
profile: fix potential race condition with cache invalidation
1 parent d12b706 commit 4fabdb5

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

profile/cache/store.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,27 @@ func (c *Cache) GetProfile(ctx context.Context, id *commonpb.UserId) (*profilepb
5656
}
5757

5858
func (c *Cache) SetDisplayName(ctx context.Context, id *commonpb.UserId, displayName string) error {
59-
c.cache.Remove(toCacheKey(id))
60-
return c.db.SetDisplayName(ctx, id, displayName)
59+
err := c.db.SetDisplayName(ctx, id, displayName)
60+
if err == nil {
61+
c.cache.Remove(toCacheKey(id))
62+
}
63+
return err
6164
}
6265

6366
func (c *Cache) LinkXAccount(ctx context.Context, userID *commonpb.UserId, xProfile *profilepb.XProfile, accessToken string) error {
64-
c.cache.Remove(toCacheKey(userID))
65-
return c.db.LinkXAccount(ctx, userID, xProfile, accessToken)
67+
err := c.db.LinkXAccount(ctx, userID, xProfile, accessToken)
68+
if err == nil {
69+
c.cache.Remove(toCacheKey(userID))
70+
}
71+
return err
6672
}
6773

6874
func (c *Cache) UnlinkXAccount(ctx context.Context, userID *commonpb.UserId, xUserID string) error {
69-
c.cache.Remove(toCacheKey(userID))
70-
return c.db.UnlinkXAccount(ctx, userID, xUserID)
75+
err := c.db.UnlinkXAccount(ctx, userID, xUserID)
76+
if err == nil {
77+
c.cache.Remove(toCacheKey(userID))
78+
}
79+
return err
7180
}
7281

7382
func (c *Cache) GetXProfile(ctx context.Context, userID *commonpb.UserId) (*profilepb.XProfile, error) {

0 commit comments

Comments
 (0)