Skip to content

Commit 43e3489

Browse files
PengYechang彭业昌
andauthored
refactor: improve local cache
* feat(xgolanglru): improve * feat(xgolanglru): improve2 * feat(xgolanglru): improve test * feat(xgolanglru): improve test --------- Co-authored-by: 彭业昌 <refrain@douyu.tv>
1 parent 7c3335f commit 43e3489

File tree

4 files changed

+140
-12
lines changed

4 files changed

+140
-12
lines changed

pkg/cache/xfreecache/v2/cache.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,16 @@ func (l *localStorage[K, V]) SetCacheMapOrigin(key string, idsNone []K, fn func(
4646

4747
// 执行函数
4848
resMap, err := fn(idsNone)
49-
if err != nil {
50-
xlog.Jupiter().Error("GetAndSetCacheMap doMap", append(args, zap.Error(err))...)
51-
return
52-
}
53-
54-
// 填入返回中
49+
// 先填入返回值,再判断错误返回
5550
if v != nil {
5651
for k, value := range resMap {
5752
v[k] = value
5853
}
5954
}
55+
if err != nil {
56+
xlog.Jupiter().Error("GetAndSetCacheMap doMap", append(args, zap.Error(err))...)
57+
return
58+
}
6059

6160
// 写入缓存
6261
for _, id := range idsNone {

pkg/cache/xfreecache/v2/cache_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package xfreecache
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1"
78
"testing"
@@ -257,3 +258,67 @@ func TestStdConfig(t *testing.T) {
257258
})
258259

259260
}
261+
262+
func TestGetAndSetDataWithError(t *testing.T) {
263+
var configStr = `
264+
[jupiter.cache]
265+
size = "128m"
266+
[jupiter.cache.test1]
267+
expire = "60s"
268+
`
269+
assert.Nil(t, conf.LoadFromReader(bytes.NewBufferString(configStr), toml.Unmarshal))
270+
271+
for i := 1; i <= 1; i++ {
272+
oneCache := StdNew[string, *helloworldv1.SayHiRequest](fmt.Sprintf("test%d", i))
273+
missCount := 0
274+
275+
tests := []struct {
276+
stu *helloworldv1.SayHiRequest
277+
}{
278+
{
279+
stu: &helloworldv1.SayHiRequest{
280+
Name: "Student 1",
281+
},
282+
},
283+
{
284+
stu: &helloworldv1.SayHiRequest{
285+
Name: "Student 2",
286+
},
287+
},
288+
{
289+
stu: &helloworldv1.SayHiRequest{
290+
Name: "Student 1",
291+
},
292+
},
293+
{
294+
stu: &helloworldv1.SayHiRequest{
295+
Name: "Student 3",
296+
},
297+
},
298+
{
299+
stu: &helloworldv1.SayHiRequest{
300+
Name: "Student 2",
301+
},
302+
},
303+
{
304+
stu: nil,
305+
},
306+
{
307+
stu: nil,
308+
},
309+
}
310+
311+
for _, tt := range tests {
312+
key := "errTest" + tt.stu.GetName()
313+
result, _ := oneCache.GetAndSetCacheData(key, tt.stu.GetName(), func() (*helloworldv1.SayHiRequest, error) {
314+
missCount++
315+
fmt.Println("local cache miss hit")
316+
return tt.stu, errors.New("this is a test error")
317+
})
318+
fmt.Println(result)
319+
assert.Equalf(t, tt.stu.GetName(), result.GetName(), "GetAndSetCacheData(%v) cache value error", key)
320+
}
321+
// 因为接口报错了,所以全部没有命中缓存
322+
assert.Equalf(t, missCount, 7, "GetAndSetCacheData miss count error")
323+
}
324+
}

pkg/cache/xgolanglru/cache.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,16 @@ func (l *localStorage[K, V]) SetCacheMapOrigin(key string, idsNone []K, fn func(
5353

5454
// 执行函数
5555
resMap, err := fn(idsNone)
56-
if err != nil {
57-
xlog.Jupiter().Error("setCacheMap doMap", append(args, zap.Error(err))...)
58-
return
59-
}
60-
61-
// 填入返回中
56+
// 先填入返回值,再判断错误返回
6257
if v != nil {
6358
for k, value := range resMap {
6459
v[k] = value
6560
}
6661
}
62+
if err != nil {
63+
xlog.Jupiter().Error("setCacheMap doMap", append(args, zap.Error(err))...)
64+
return
65+
}
6766

6867
// 写入缓存
6968
for _, id := range idsNone {

pkg/cache/xgolanglru/cache_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package xgolanglru
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1"
78
"testing"
@@ -258,3 +259,67 @@ func TestStdConfig(t *testing.T) {
258259
})
259260

260261
}
262+
263+
func TestGetAndSetDataWithError(t *testing.T) {
264+
var configStr = `
265+
[jupiter.cache]
266+
size = "128m"
267+
[jupiter.cache.test1]
268+
expire = "60s"
269+
`
270+
assert.Nil(t, conf.LoadFromReader(bytes.NewBufferString(configStr), toml.Unmarshal))
271+
272+
for i := 1; i <= 1; i++ {
273+
oneCache := StdNew[string, *helloworldv1.SayHiRequest](fmt.Sprintf("test%d", i))
274+
missCount := 0
275+
276+
tests := []struct {
277+
stu *helloworldv1.SayHiRequest
278+
}{
279+
{
280+
stu: &helloworldv1.SayHiRequest{
281+
Name: "Student 1",
282+
},
283+
},
284+
{
285+
stu: &helloworldv1.SayHiRequest{
286+
Name: "Student 2",
287+
},
288+
},
289+
{
290+
stu: &helloworldv1.SayHiRequest{
291+
Name: "Student 1",
292+
},
293+
},
294+
{
295+
stu: &helloworldv1.SayHiRequest{
296+
Name: "Student 3",
297+
},
298+
},
299+
{
300+
stu: &helloworldv1.SayHiRequest{
301+
Name: "Student 2",
302+
},
303+
},
304+
{
305+
stu: nil,
306+
},
307+
{
308+
stu: nil,
309+
},
310+
}
311+
312+
for _, tt := range tests {
313+
key := "errTest" + tt.stu.GetName()
314+
result, _ := oneCache.GetAndSetCacheData(key, tt.stu.GetName(), func() (*helloworldv1.SayHiRequest, error) {
315+
missCount++
316+
fmt.Println("local cache miss hit")
317+
return tt.stu, errors.New("this is a test error")
318+
})
319+
fmt.Println(result)
320+
assert.Equalf(t, tt.stu.GetName(), result.GetName(), "GetAndSetCacheData(%v) cache value error", key)
321+
}
322+
// 因为接口报错了,所以全部没有命中缓存
323+
assert.Equalf(t, missCount, 7, "GetAndSetCacheData miss count error")
324+
}
325+
}

0 commit comments

Comments
 (0)