Skip to content

feat: add GetCached method to report cache hit status#178

Open
toller892 wants to merge 1 commit into
golang:masterfrom
toller892:feature/get-cached
Open

feat: add GetCached method to report cache hit status#178
toller892 wants to merge 1 commit into
golang:masterfrom
toller892:feature/get-cached

Conversation

@toller892

Copy link
Copy Markdown

Summary

Adds a GetCached method to Group that returns whether the value was served from the local cache, addressing #61.

Motivation

Currently, Group.Get provides no way for callers to distinguish cache hits from cache misses. The only workaround is to compare Stats.CacheHits before and after the call, which is awkward and not goroutine-safe.

Implementation

  • New exported method: GetCached(ctx, key, dest) (cached bool, err error)
  • Internally refactors Get to delegate to a private getCached helper
  • Get behavior is 100% unchanged — this is purely additive
  • The cached return value is true only when the value was found in mainCache or hotCache

API

// GetCached acts like Get but additionally reports whether the
// value was served from the local cache.
func (g *Group) GetCached(ctx context.Context, key string, dest Sink) (cached bool, err error)

Tests

Added TestGetCached covering:

  • First call returns cached=false (cache miss)
  • Second call returns cached=true (cache hit)
  • Nil dest returns error

All existing tests pass unchanged.

Fixes #61

GetCached acts like Get but additionally returns a bool indicating
whether the value was served from the local cache. This allows callers
to distinguish cache hits from misses without relying on Stats counters.

The existing Get method is refactored to delegate to the internal
getCached helper, preserving full backward compatibility.

Fixes golang#61
@google-cla

google-cla Bot commented Jun 2, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support whether the returned value was cached or not

1 participant