From 0c931b4a8efec0150e3f42708aa6d261370f866f Mon Sep 17 00:00:00 2001 From: Alan Mock Date: Wed, 22 Feb 2023 00:10:50 -0700 Subject: [PATCH] Add GetWithPopulated --- groupcache.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/groupcache.go b/groupcache.go index 3d1ebec..1fabf2c 100644 --- a/groupcache.go +++ b/groupcache.go @@ -264,6 +264,34 @@ func (g *Group) Get(ctx context.Context, key string, dest Sink) error { return setSinkView(dest, value) } +func (g *Group) GetWithPopulated(ctx context.Context, key string, dest Sink) (bool, error) { + g.peersOnce.Do(g.initPeers) + g.Stats.Gets.Add(1) + if dest == nil { + return false, errors.New("groupcache: nil dest Sink") + } + value, cacheHit := g.lookupCache(key) + + if cacheHit { + g.Stats.CacheHits.Add(1) + return false, setSinkView(dest, value) + } + + // Optimization to avoid double unmarshalling or copying: keep + // track of whether the dest was already populated. One caller + // (if local) will set this; the losers will not. The common + // case will likely be one caller. + destPopulated := false + value, destPopulated, err := g.load(ctx, key, dest) + if err != nil { + return false, err + } + if destPopulated { + return true, nil + } + return false, setSinkView(dest, value) +} + func (g *Group) Set(ctx context.Context, key string, value []byte, expire time.Time, hotCache bool) error { g.peersOnce.Do(g.initPeers)