From ceebf94ab09b5c529dbd855d3b14f801d8a13ccf Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 17 Oct 2023 23:20:51 -0700 Subject: [PATCH] set cache locally if owner is not reachable --- groupcache.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/groupcache.go b/groupcache.go index 541e89a..0031397 100644 --- a/groupcache.go +++ b/groupcache.go @@ -243,7 +243,6 @@ func (g *Group) Get(ctx context.Context, key string, dest Sink) error { return errors.New("groupcache: nil dest Sink") } value, cacheHit := g.lookupCache(key) - if cacheHit { g.Stats.CacheHits.Add(1) return setSinkView(dest, value) @@ -276,6 +275,8 @@ func (g *Group) Set(ctx context.Context, key string, value []byte, expire time.T owner, ok := g.peers.PickPeer(key) if ok { if err := g.setFromPeer(ctx, owner, key, value, expire); err != nil { + // In-case of peer failure, set it locally. + g.localSet(key, value, expire, &g.mainCache) return nil, err } // TODO(thrawn01): Not sure if this is useful outside of tests... @@ -283,10 +284,9 @@ func (g *Group) Set(ctx context.Context, key string, value []byte, expire time.T if hotCache { g.localSet(key, value, expire, &g.hotCache) } - return nil, nil + } else { + g.localSet(key, value, expire, &g.mainCache) } - // We own this key - g.localSet(key, value, expire, &g.mainCache) return nil, nil }) return err