feat: add GetCached method to report cache hit status#178
Open
toller892 wants to merge 1 commit into
Open
Conversation
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
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
GetCachedmethod toGroupthat returns whether the value was served from the local cache, addressing #61.Motivation
Currently,
Group.Getprovides no way for callers to distinguish cache hits from cache misses. The only workaround is to compareStats.CacheHitsbefore and after the call, which is awkward and not goroutine-safe.Implementation
GetCached(ctx, key, dest) (cached bool, err error)Getto delegate to a privategetCachedhelperGetbehavior is 100% unchanged — this is purely additivecachedreturn value istrueonly when the value was found inmainCacheorhotCacheAPI
Tests
Added
TestGetCachedcovering:cached=false(cache miss)cached=true(cache hit)All existing tests pass unchanged.
Fixes #61