Skip to content

Commit d494cc3

Browse files
authored
Fix nodeinfo caching and prevent NPE if cache non-existent (#19721)
Extract from #19703 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 00a981d commit d494cc3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

routers/api/v1/misc/nodeinfo.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ func NodeInfo(ctx *context.APIContext) {
3030

3131
nodeInfoUsage := structs.NodeInfoUsage{}
3232
if setting.Federation.ShareUserStatistics {
33-
info, ok := ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
34-
if !ok {
33+
cached := false
34+
if setting.CacheService.Enabled {
35+
nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
36+
}
37+
if !cached {
3538
usersTotal := int(user_model.CountUsers(nil))
3639
now := time.Now()
3740
timeOneMonthAgo := now.AddDate(0, -1, 0).Unix()
@@ -42,7 +45,7 @@ func NodeInfo(ctx *context.APIContext) {
4245
allIssues, _ := models.CountIssues(&models.IssuesOptions{})
4346
allComments, _ := models.CountComments(&models.FindCommentsOptions{})
4447

45-
info = structs.NodeInfoUsage{
48+
nodeInfoUsage = structs.NodeInfoUsage{
4649
Users: structs.NodeInfoUsageUsers{
4750
Total: usersTotal,
4851
ActiveMonth: usersActiveMonth,
@@ -51,12 +54,13 @@ func NodeInfo(ctx *context.APIContext) {
5154
LocalPosts: int(allIssues),
5255
LocalComments: int(allComments),
5356
}
54-
if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
55-
ctx.InternalServerError(err)
56-
return
57+
if setting.CacheService.Enabled {
58+
if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
59+
ctx.InternalServerError(err)
60+
return
61+
}
5762
}
5863
}
59-
nodeInfoUsage = info
6064
}
6165

6266
nodeInfo := &structs.NodeInfo{

0 commit comments

Comments
 (0)