Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit a32e08c

Browse files
committed
update error cache
1 parent ff798ab commit a32e08c

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

cache.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ func SetJSONCache(ctx iris.Context, key string, value interface{}) error {
5353
return SetCache(ctx, key, string(val))
5454
}
5555

56-
func GetJSONCache(ctx iris.Context, key string) (value iris.Map, ok bool) {
56+
func GetJSONCache(ctx iris.Context, key string) (value AnalysisData, ok bool) {
5757
val, ok := GetCache(ctx, key)
5858
if !ok {
59-
return nil, false
59+
return AnalysisData{}, false
6060
}
6161
err := json.Unmarshal([]byte(val), &value)
6262
if err != nil {
63-
return nil, false
63+
return AnalysisData{}, false
6464
}
6565
return value, true
6666
}
@@ -79,22 +79,29 @@ func CachedHandler(h iris.Handler, params ...string) iris.Handler {
7979
return func(ctx iris.Context) {
8080
key := GenerateKey(ctx, params...)
8181

82-
result, ok := GetJSONCache(ctx, key)
82+
data, ok := GetJSONCache(ctx, key)
8383
if ok {
84-
ctx.JSON(result)
85-
return
84+
EndBody(ctx, data)
85+
} else {
86+
h(ctx)
8687
}
88+
}
89+
}
8790

88-
h(ctx)
91+
func EndBody(ctx iris.Context, data AnalysisData) {
92+
if data.Err != nil {
93+
ThrowError(ctx, data.Err.Error(), data.Code)
94+
} else {
95+
ctx.JSON(data.Data)
8996
}
9097
}
9198

92-
func EndBody(ctx iris.Context, value iris.Map, params ...string) {
93-
err := SetJSONCache(ctx, GenerateKey(ctx, params...), value)
99+
func EndBodyWithCache(ctx iris.Context, data AnalysisData, params ...string) {
100+
err := SetJSONCache(ctx, GenerateKey(ctx, params...), data)
94101
if err != nil {
95102
logger.Errorf("Failed to set cache: %s", err.Error())
96103
ThrowError(ctx, err.Error(), iris.StatusInternalServerError)
97104
return
98105
}
99-
ctx.JSON(value)
106+
EndBody(ctx, data)
100107
}

server.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,11 @@ func RunServer() {
1717
func UserAPI(ctx iris.Context) {
1818
username := ctx.Params().Get("username")
1919
data := AnalysisUser(username)
20-
if data.Err != nil {
21-
ThrowError(ctx, data.Err.Error(), data.Code)
22-
} else {
23-
EndBody(ctx, data.Data, "username")
24-
}
20+
EndBodyWithCache(ctx, data, "username")
2521
}
2622

2723
func RepoAPI(ctx iris.Context) {
2824
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
2925
data := AnalysisRepo(username, repo)
30-
if data.Err != nil {
31-
ThrowError(ctx, data.Err.Error(), data.Code)
32-
} else {
33-
EndBody(ctx, data.Data, "username", "repo")
34-
}
26+
EndBodyWithCache(ctx, data, "username", "repo")
3527
}

0 commit comments

Comments
 (0)