8000 Add cache hit field in Item by zacscoding · Pull Request #80 · go-redis/cache · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add cache hit field in Item #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type Item struct {

// SkipLocalCache skips local cache as if it is not set.
SkipLocalCache bool

// CacheHit is true if Value is found in the cache.
CacheHit bool
}

func (item *Item) Context() context.Context {
Expand Down Expand Up @@ -259,6 +262,7 @@ func (cd *Cache) Once(item *Item) error {
if err != nil {
return err
}
item.CacheHit = cached

if item.Value == nil || len(b) == 0 {
return nil
Expand Down
24 changes: 24 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ var _ = Describe("Cache", func() {
Expect(callCount).To(Equal(int64(1)))
})

It("works with CacheHit", func() {
key := "cache-hit-test"
do := func(item *cache.Item) (interface{}, error) {
return "hello", nil
}
var value string
item := cache.Item{
Ctx: ctx,
Key: key,
Value: &value,
Do: do,
}

err := mycache.Once(&item)

Expect(err).NotTo(HaveOccurred())
Expect(item.CacheHit).To(Equal(false))

err = mycache.Once(&item)

Expect(err).NotTo(HaveOccurred())
Expect(item.CacheHit).To(Equal(true))
})

It("works with ptr and non-ptr", func() {
var callCount int64
perform(100, func(int) {
Expand Down
0