Skip to content

Commit

Permalink
Merge pull request #37 from mgtv-tech/fix_refresh
Browse files Browse the repository at this point in the history
Fix refresh
  • Loading branch information
daoshenzzg authored Oct 14, 2024
2 parents d5a844a + b2e7e0a commit f92890f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ func (c *jetCache) externalLoad(ctx context.Context, task *refreshTask, now time
return
}

ok, err := c.remote.SetNX(ctx, lockKey, strconv.FormatInt(now.Unix(), 10), c.refreshDuration)
// issues: https://github.com/mgtv-tech/jetcache-go/issues/36
lockTimeout := c.refreshDuration - 10*time.Millisecond
ok, err := c.remote.SetNX(ctx, lockKey, strconv.FormatInt(now.Unix(), 10), lockTimeout)
if err != nil {
logger.Error("externalLoad#c.remote.setNX(%s) error(%v)", lockKey, err)
return
Expand Down
2 changes: 0 additions & 2 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,6 @@ var _ = Describe("Cache", func() {
err = cache.Get(ctx, key, &value)
Expect(err).NotTo(HaveOccurred())
Expect(value).To(Equal("V2"))

time.Sleep(2 * refreshDuration)
})

It("refresh err", func() {
Expand Down
4 changes: 4 additions & 0 deletions cacheopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
defaultCodec = msgpack.Name
defaultRandSourceIdLen = 16
defaultEventChBufSize = 100
minEffectRefreshDuration = time.Second
maxOffset = 10 * time.Second
)

Expand Down Expand Up @@ -90,6 +91,9 @@ func newOptions(opts ...Option) Options {
if o.refreshConcurrency <= 0 {
o.refreshConcurrency = defaultRefreshConcurrency
}
if o.refreshDuration > 0 && o.refreshDuration < minEffectRefreshDuration {
o.refreshDuration = minEffectRefreshDuration
}
if o.stopRefreshAfterLastAccess <= 0 {
o.stopRefreshAfterLastAccess = o.refreshDuration + time.Second
}
Expand Down
32 changes: 31 additions & 1 deletion cacheopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@ func TestCacheOptions(t *testing.T) {
assert.Equal(t, maxOffset, o.offset)
})

t.Run("with refresh duration", func(t *testing.T) {
o := newOptions(WithRefreshDuration(time.Second))
assert.Equal(t, time.Second, o.refreshDuration)
})

t.Run("with refresh concurrency", func(t *testing.T) {
o := newOptions(WithRefreshConcurrency(16))
assert.Equal(t, defaultNotFoundExpiry, o.notFoundExpiry)
assert.Equal(t, 16, o.refreshConcurrency)
assert.Equal(t, defaultCodec, o.codec)
})

t.Run("with mockDecode", func(t *testing.T) {
t.Run("with mock decode", func(t *testing.T) {
o := newOptions(WithCodec(json.Name))
assert.Equal(t, defaultNotFoundExpiry, o.notFoundExpiry)
assert.Equal(t, defaultRefreshConcurrency, o.refreshConcurrency)
Expand Down Expand Up @@ -103,3 +108,28 @@ func TestCacheOptions(t *testing.T) {
assert.NotNil(t, o.eventHandler)
})
}

func TestCacheOptionsRefreshDuration(t *testing.T) {
tests := []struct {
input time.Duration
expect time.Duration
}{
{
input: 0,
expect: 0,
},
{
input: time.Millisecond,
expect: minEffectRefreshDuration,
},
{
input: time.Minute,
expect: time.Minute,
},
}

for _, v := range tests {
o := newOptions(WithRefreshDuration(v.input))
assert.Equal(t, v.expect, o.refreshDuration)
}
}

0 comments on commit f92890f

Please sign in to comment.