summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/misc/nodeinfo.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-05-16 08:36:57 +0100
committerGitHub <noreply@github.com>2022-05-16 15:36:57 +0800
commitd494cc335633ff838eaea547438b95e753aeec0f (patch)
tree414722886433c31a0fdcd9beff1c6e666aadbf84 /routers/api/v1/misc/nodeinfo.go
parent00a981d341dfa62d2a9667b01500820ec7ddf19a (diff)
downloadgitea-d494cc335633ff838eaea547438b95e753aeec0f.tar.gz
gitea-d494cc335633ff838eaea547438b95e753aeec0f.zip
Fix nodeinfo caching and prevent NPE if cache non-existent (#19721)
Extract from #19703 Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers/api/v1/misc/nodeinfo.go')
-rw-r--r--routers/api/v1/misc/nodeinfo.go18
1 files changed, 11 insertions, 7 deletions
diff --git a/routers/api/v1/misc/nodeinfo.go b/routers/api/v1/misc/nodeinfo.go
index ce1f9ec0f7..c786544e14 100644
--- a/routers/api/v1/misc/nodeinfo.go
+++ b/routers/api/v1/misc/nodeinfo.go
@@ -30,8 +30,11 @@ func NodeInfo(ctx *context.APIContext) {
nodeInfoUsage := structs.NodeInfoUsage{}
if setting.Federation.ShareUserStatistics {
- info, ok := ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
- if !ok {
+ cached := false
+ if setting.CacheService.Enabled {
+ nodeInfoUsage, cached = ctx.Cache.Get(cacheKeyNodeInfoUsage).(structs.NodeInfoUsage)
+ }
+ if !cached {
usersTotal := int(user_model.CountUsers(nil))
now := time.Now()
timeOneMonthAgo := now.AddDate(0, -1, 0).Unix()
@@ -42,7 +45,7 @@ func NodeInfo(ctx *context.APIContext) {
allIssues, _ := models.CountIssues(&models.IssuesOptions{})
allComments, _ := models.CountComments(&models.FindCommentsOptions{})
- info = structs.NodeInfoUsage{
+ nodeInfoUsage = structs.NodeInfoUsage{
Users: structs.NodeInfoUsageUsers{
Total: usersTotal,
ActiveMonth: usersActiveMonth,
@@ -51,12 +54,13 @@ func NodeInfo(ctx *context.APIContext) {
LocalPosts: int(allIssues),
LocalComments: int(allComments),
}
- if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
- ctx.InternalServerError(err)
- return
+ if setting.CacheService.Enabled {
+ if err := ctx.Cache.Put(cacheKeyNodeInfoUsage, nodeInfoUsage, 180); err != nil {
+ ctx.InternalServerError(err)
+ return
+ }
}
}
- nodeInfoUsage = info
}
nodeInfo := &structs.NodeInfo{