Skip to content

Commit f92890f

Browse files
authored
Merge pull request #37 from mgtv-tech/fix_refresh
Fix refresh
2 parents d5a844a + b2e7e0a commit f92890f

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,9 @@ func (c *jetCache) externalLoad(ctx context.Context, task *refreshTask, now time
483483
return
484484
}
485485

486-
ok, err := c.remote.SetNX(ctx, lockKey, strconv.FormatInt(now.Unix(), 10), c.refreshDuration)
486+
// issues: https://github.com/mgtv-tech/jetcache-go/issues/36
487+
lockTimeout := c.refreshDuration - 10*time.Millisecond
488+
ok, err := c.remote.SetNX(ctx, lockKey, strconv.FormatInt(now.Unix(), 10), lockTimeout)
487489
if err != nil {
488490
logger.Error("externalLoad#c.remote.setNX(%s) error(%v)", lockKey, err)
489491
return

cache_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,6 @@ var _ = Describe("Cache", func() {
481481
err = cache.Get(ctx, key, &value)
482482
Expect(err).NotTo(HaveOccurred())
483483
Expect(value).To(Equal("V2"))
484-
485-
time.Sleep(2 * refreshDuration)
486484
})
487485

488486
It("refresh err", func() {

cacheopt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
defaultCodec = msgpack.Name
1919
defaultRandSourceIdLen = 16
2020
defaultEventChBufSize = 100
21+
minEffectRefreshDuration = time.Second
2122
maxOffset = 10 * time.Second
2223
)
2324

@@ -90,6 +91,9 @@ func newOptions(opts ...Option) Options {
9091
if o.refreshConcurrency <= 0 {
9192
o.refreshConcurrency = defaultRefreshConcurrency
9293
}
94+
if o.refreshDuration > 0 && o.refreshDuration < minEffectRefreshDuration {
95+
o.refreshDuration = minEffectRefreshDuration
96+
}
9397
if o.stopRefreshAfterLastAccess <= 0 {
9498
o.stopRefreshAfterLastAccess = o.refreshDuration + time.Second
9599
}

cacheopt_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ func TestCacheOptions(t *testing.T) {
5656
assert.Equal(t, maxOffset, o.offset)
5757
})
5858

59+
t.Run("with refresh duration", func(t *testing.T) {
60+
o := newOptions(WithRefreshDuration(time.Second))
61+
assert.Equal(t, time.Second, o.refreshDuration)
62+
})
63+
5964
t.Run("with refresh concurrency", func(t *testing.T) {
6065
o := newOptions(WithRefreshConcurrency(16))
6166
assert.Equal(t, defaultNotFoundExpiry, o.notFoundExpiry)
6267
assert.Equal(t, 16, o.refreshConcurrency)
6368
assert.Equal(t, defaultCodec, o.codec)
6469
})
6570

66-
t.Run("with mockDecode", func(t *testing.T) {
71+
t.Run("with mock decode", func(t *testing.T) {
6772
o := newOptions(WithCodec(json.Name))
6873
assert.Equal(t, defaultNotFoundExpiry, o.notFoundExpiry)
6974
assert.Equal(t, defaultRefreshConcurrency, o.refreshConcurrency)
@@ -103,3 +108,28 @@ func TestCacheOptions(t *testing.T) {
103108
assert.NotNil(t, o.eventHandler)
104109
})
105110
}
111+
112+
func TestCacheOptionsRefreshDuration(t *testing.T) {
113+
tests := []struct {
114+
input time.Duration
115+
expect time.Duration
116+
}{
117+
{
118+
input: 0,
119+
expect: 0,
120+
},
121+
{
122+
input: time.Millisecond,
123+
expect: minEffectRefreshDuration,
124+
},
125+
{
126+
input: time.Minute,
127+
expect: time.Minute,
128+
},
129+
}
130+
131+
for _, v := range tests {
132+
o := newOptions(WithRefreshDuration(v.input))
133+
assert.Equal(t, v.expect, o.refreshDuration)
134+
}
135+
}

0 commit comments

Comments
 (0)