Skip to content

Commit

Permalink
Merge pull request #4 from daoshenzzg/dev
Browse files Browse the repository at this point in the history
feat: DoFunc with context
  • Loading branch information
daoshenzzg authored Nov 24, 2023
2 parents 8cafca3 + a18bcca commit 3d5c814
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func Example_advancedUsage() {
ctx := context.TODO()
key := util.JoinAny(":", "mykey", 1)
obj := new(object)
if err := mycache.Once(ctx, key, cache.Value(obj), cache.Refresh(true), cache.Do(func() (interface{}, error) {
if err := mycache.Once(ctx, key, cache.Value(obj), cache.Refresh(true), cache.Do(func(ctx context.Context) (interface{}, error) {
return mockDBGetObject(1)
})); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func Example_advancedUsage() {
ctx := context.TODO()
key := util.JoinAny(":", "mykey", 1)
obj := new(object)
if err := mycache.Once(ctx, key, cache.Value(obj), cache.Refresh(true), cache.Do(func() (interface{}, error) {
if err := mycache.Once(ctx, key, cache.Value(obj), cache.Refresh(true), cache.Do(func(ctx context.Context) (interface{}, error) {
return mockDBGetObject(1)
})); err != nil {
panic(err)
Expand Down
8 changes: 4 additions & 4 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func BenchmarkOnceWithTinyLFU(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var dst object
err := cache.Once(context.TODO(), "bench-once", Value(&dst), Do(func() (interface{}, error) {
err := cache.Once(context.TODO(), "bench-once", Value(&dst), Do(func(context.Context) (interface{}, error) {
return obj, nil
}))
if err != nil {
Expand Down Expand Up @@ -89,7 +89,7 @@ func BenchmarkOnceWithFreeCache(b *testing.B) {
for pb.Next() {
var dst object
err := cache.Once(context.TODO(), "bench-once", Value(&dst),
Do(func() (interface{}, error) {
Do(func(context.Context) (interface{}, error) {
return obj, nil
}))
if err != nil {
Expand Down Expand Up @@ -140,7 +140,7 @@ func BenchmarkOnceWithStats(b *testing.B) {
for pb.Next() {
var dst object
err := cache.Once(context.TODO(), "bench-once_"+strconv.Itoa(rand.Intn(256)),
Value(&dst), Do(func() (interface{}, error) {
Value(&dst), Do(func(context.Context) (interface{}, error) {
time.Sleep(50 * time.Millisecond)
return obj, nil
}))
Expand Down Expand Up @@ -168,7 +168,7 @@ func BenchmarkOnceRefreshWithStats(b *testing.B) {
for pb.Next() {
var dst object
err := cache.Once(context.TODO(), "bench-refresh_"+strconv.Itoa(rand.Intn(256)),
Value(&dst), Do(func() (interface{}, error) {
Value(&dst), Do(func(context.Context) (interface{}, error) {
time.Sleep(50 * time.Millisecond)
return obj, nil
}),
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (c *jetCache) externalLoad(ctx context.Context, task *refreshTask, now time
return
}
} else if c.local != nil {
// If this coroutine fails to acquire the concurrent lock, it needs to wait briefly (delay) to trigger a refresh.
// If this goroutine fails to acquire the concurrent lock, it needs to wait briefly (delay) to trigger a refresh.
// This way, it can directly fetch the origin result from Redis and refresh it locally.
// The maximum concurrency here refers to the number of web machine instances, and the probability of
// concurrent processing is actually not high. time.AfterFunc can be understood as a fallback mechanism to
Expand Down
26 changes: 13 additions & 13 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var _ = Describe("Cache", func() {
err = nilCache.Delete(ctx, "key")
Expect(err).To(Equal(ErrRemoteLocalBothNil))

err = nilCache.Set(ctx, "key", Do(func() (interface{}, error) {
err = nilCache.Set(ctx, "key", Do(func(context.Context) (interface{}, error) {
return "getValue", nil
}))
Expect(err).To(Equal(ErrRemoteLocalBothNil))
Expand Down Expand Up @@ -201,7 +201,7 @@ var _ = Describe("Cache", func() {
Describe("Once func", func() {
It("works with err not found", func() {
key := "cache-err-not-found"
do := func() (interface{}, error) {
do := func(context.Context) (interface{}, error) {
return nil, errTestNotFound
}
var value string
Expand All @@ -216,7 +216,7 @@ var _ = Describe("Cache", func() {
}

_ = cache.Set(ctx, key, Value(value), Do(do))
do = func() (interface{}, error) {
do = func(context.Context) (interface{}, error) {
return nil, nil
}
err = cache.Once(ctx, key, Value(&value), Do(do))
Expand All @@ -226,7 +226,7 @@ var _ = Describe("Cache", func() {

_ = cache.Delete(context.Background(), key)
errAny := errors.New("any")
do = func() (interface{}, error) {
do = func(context.Context) (interface{}, error) {
return nil, errAny
}
err = cache.Once(ctx, key, Value(&value), Do(do))
Expand All @@ -236,7 +236,7 @@ var _ = Describe("Cache", func() {
It("works without value and error result", func() {
var callCount int64
perform(100, func(int) {
err := cache.Once(ctx, key, Do(func() (interface{}, error) {
err := cache.Once(ctx, key, Do(func(context.Context) (interface{}, error) {
time.Sleep(100 * time.Millisecond)
atomic.AddInt64(&callCount, 1)
return nil, errors.New("error stub")
Expand All @@ -250,7 +250,7 @@ var _ = Describe("Cache", func() {
var callCount int64
do := func(sleep time.Duration) (int, error) {
var n int
err := cache.Once(ctx, key, Value(&n), Do(func() (interface{}, error) {
err := cache.Once(ctx, key, Value(&n), Do(func(context.Context) (interface{}, error) {
time.Sleep(sleep)

n := atomic.AddInt64(&callCount, 1)
Expand Down Expand Up @@ -284,7 +284,7 @@ var _ = Describe("Cache", func() {
key := "skip-set"

var value string
err := cache.Once(ctx, key, Value(&value), TTL(-1), Do(func() (interface{}, error) {
err := cache.Once(ctx, key, Value(&value), TTL(-1), Do(func(context.Context) (interface{}, error) {
return "hello", nil
}))
Expect(err).NotTo(HaveOccurred())
Expand All @@ -307,7 +307,7 @@ var _ = Describe("Cache", func() {
err error
)
err = cache.Once(ctx, key, Value(&value), TTL(time.Minute), Refresh(true),
Do(func() (interface{}, error) {
Do(func(context.Context) (interface{}, error) {
if atomic.AddInt64(&callCount, 1) == 1 {
return "V1", nil
}
Expand Down Expand Up @@ -337,7 +337,7 @@ var _ = Describe("Cache", func() {
err error
)
err = cache.Once(ctx, key, Value(&value), TTL(time.Minute), Refresh(true),
Do(func() (interface{}, error) {
Do(func(context.Context) (interface{}, error) {
if atomic.AddInt64(&callCount, 1) == 1 {
return "", errors.New("any")
}
Expand Down Expand Up @@ -367,7 +367,7 @@ var _ = Describe("Cache", func() {
err error
)
err = jetCache.Once(ctx, key, Value(&value), TTL(time.Minute), Refresh(true),
Do(func() (interface{}, error) {
Do(func(context.Context) (interface{}, error) {
return "V1", nil
}))
Expect(err).NotTo(HaveOccurred())
Expand All @@ -390,7 +390,7 @@ var _ = Describe("Cache", func() {
callCount int64
jetCache = cache.(*jetCache)
key = util.JoinAny(":", cache.CacheType(), "K1")
doFunc = func() (interface{}, error) {
doFunc = func(context.Context) (interface{}, error) {
if atomic.AddInt64(&callCount, 1) == 1 {
return "V1", nil
}
Expand Down Expand Up @@ -427,7 +427,7 @@ var _ = Describe("Cache", func() {
jetCache = cache.(*jetCache)
key = util.JoinAny(":", cache.CacheType(), "K1")
lockKey = fmt.Sprintf("%s%s", key, lockKeySuffix)
doFunc = func() (interface{}, error) {
doFunc = func(context.Context) (interface{}, error) {
return "V1", nil
}
value string
Expand Down Expand Up @@ -466,7 +466,7 @@ var _ = Describe("Cache", func() {
item := &item{
key: key,
ttl: time.Minute,
do: func() (interface{}, error) {
do: func(context.Context) (interface{}, error) {
return "V1", nil
},
refresh: true,
Expand Down
2 changes: 1 addition & 1 deletion example_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Example_advancedUsage() {
ctx := context.TODO()
key := util.JoinAny(":", "mykey", 1)
obj := new(object)
if err := mycache.Once(ctx, key, cache.Value(obj), cache.Refresh(true), cache.Do(func() (interface{}, error) {
if err := mycache.Once(ctx, key, cache.Value(obj), cache.Refresh(true), cache.Do(func(ctx context.Context) (interface{}, error) {
return mockDBGetObject(1)
})); err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type (
// ItemOption defines the method to customize an Options.
ItemOption func(o *item)
// DoFunc returns getValue to be cached.
DoFunc func() (interface{}, error)
DoFunc func(ctx context.Context) (interface{}, error)

item struct {
ctx context.Context
Expand Down Expand Up @@ -98,7 +98,7 @@ func (item *item) Context() context.Context {

func (item *item) getValue() (interface{}, error) {
if item.do != nil {
return item.do()
return item.do(item.Context())
}
if item.value != nil {
return item.value, nil
Expand Down
2 changes: 1 addition & 1 deletion item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestItemOptions(t *testing.T) {
t.Run("with item options", func(t *testing.T) {
o := newItemOptions(context.TODO(), "key", Value("getValue"),
TTL(time.Minute), SetXX(true), SetNX(true), SkipLocal(true),
Refresh(true), Do(func() (interface{}, error) {
Refresh(true), Do(func(context.Context) (interface{}, error) {
return "any", nil
}))
assert.Equal(t, "getValue", o.value)
Expand Down

0 comments on commit 3d5c814

Please sign in to comment.