Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

add dashboard cache

aichy12621b7560f2c1c
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index 49e980eb..e7ade4d0 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -152,7 +152,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
questionService := service.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, revisionService, metaService, collectionCommon, answerActivityService)
questionController := controller.NewQuestionController(questionService, rankService)
answerService := service.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo)
- dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configRepo, siteInfoCommonService)
+ dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configRepo, siteInfoCommonService, dataData)
answerController := controller.NewAnswerController(answerService, rankService, dashboardService)
searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon)
searchService := service.NewSearchService(searchRepo, tagRepo, userCommon, followRepo)
diff --git a/internal/controller/cron_controller.go b/internal/controller/cron_controller.go
new file mode 100644
index 00000000..b0b429f8
--- /dev/null
+++ b/internal/controller/cron_controller.go
@@ -0,0 +1 @@
+package controller
diff --git a/internal/controller/dashboard_controller.go b/internal/controller/dashboard_controller.go
index 6ee50f4c..a525adeb 100644
--- a/internal/controller/dashboard_controller.go
+++ b/internal/controller/dashboard_controller.go
@@ -29,7 +29,7 @@ func NewDashboardController(
// @Router /answer/admin/api/dashboard [get]
// @Success 200 {object} handler.RespBody
func (ac *DashboardController) DashboardInfo(ctx *gin.Context) {
- info, err := ac.dashboardService.Statistical(ctx)
+ info, err := ac.dashboardService.StatisticalByCache(ctx)
handler.HandleResponse(ctx, err, gin.H{
"info": info,
})
diff --git a/internal/schema/dashboard_schema.go b/internal/schema/dashboard_schema.go
index 2f5b3622..d08ee2b2 100644
--- a/internal/schema/dashboard_schema.go
+++ b/internal/schema/dashboard_schema.go
@@ -4,6 +4,11 @@ import "time"
var AppStartTime time.Time
+const (
+ DashBoardCachekey = "answer@dashboard"
+ DashBoardCacheTime = 31 * time.Minute
+)
+
type DashboardInfo struct {
QuestionCount int64 `json:"question_count"`
AnswerCount int64 `json:"answer_count"`
diff --git a/internal/service/dashboard/dashboard_service.go b/internal/service/dashboard/dashboard_service.go
index aa876507..f1ce90ad 100644
--- a/internal/service/dashboard/dashboard_service.go
+++ b/internal/service/dashboard/dashboard_service.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/answerdev/answer/internal/base/constant"
+ "github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/activity_common"
@@ -33,6 +34,7 @@ type DashboardService struct {
reportRepo report_common.ReportRepo
configRepo config.ConfigRepo
siteInfoService *siteinfo_common.SiteInfoCommonService
+ data *data.Data
}
func NewDashboardService(
@@ -44,6 +46,7 @@ func NewDashboardService(
reportRepo report_common.ReportRepo,
configRepo config.ConfigRepo,
siteInfoService *siteinfo_common.SiteInfoCommonService,
+ data *data.Data,
) *DashboardService {
return &DashboardService{
questionRepo: questionRepo,
@@ -54,9 +57,40 @@ func NewDashboardService(
reportRepo: reportRepo,
configRepo: configRepo,
siteInfoService: siteInfoService,
+ data: data,
}
}
+func (ds *DashboardService) StatisticalByCache(ctx context.Context) (*schema.DashboardInfo, error) {
+ ds.SetCache(ctx)
+ dashboardInfo := &schema.DashboardInfo{}
+ infoStr, err := ds.data.Cache.GetString(ctx, schema.DashBoardCachekey)
+ if err != nil {
+ return dashboardInfo, err
+ }
+ err = json.Unmarshal([]byte(infoStr), dashboardInfo)
+ if err != nil {
+ return dashboardInfo, err
+ }
+ return dashboardInfo, nil
+}
+
+func (ds *DashboardService) SetCache(ctx context.Context) error {
+ info, err := ds.Statistical(ctx)
+ if err != nil {
+ return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
+ }
+ infoStr, err := json.Marshal(info)
+ if err != nil {
+ return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
+ }
+ err = ds.data.Cache.SetString(ctx, schema.DashBoardCachekey, string(infoStr), schema.DashBoardCacheTime)
+ if err != nil {
+ return errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
+ }
+ return nil
+}
+
// Statistical
func (ds *DashboardService) Statistical(ctx context.Context) (*schema.DashboardInfo, error) {
dashboardInfo := &schema.DashboardInfo{}