1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: add caching to badge endpoint

chore: add type index for summary items
This commit is contained in:
Ferdinand Mütsch 2021-03-24 21:49:42 +01:00
parent 5ca9a6a8be
commit 2d1010e9d9
2 changed files with 12 additions and 1 deletions

View File

@ -35,7 +35,7 @@ type SummaryItem struct {
ID uint `json:"-" gorm:"primary_key"` ID uint `json:"-" gorm:"primary_key"`
Summary *Summary `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` Summary *Summary `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
SummaryID uint `json:"-"` SummaryID uint `json:"-"`
Type uint8 `json:"-"` Type uint8 `json:"-" gorm:"index:idx_type"`
Key string `json:"key"` Key string `json:"key"`
Total time.Duration `json:"total" swaggertype:"primitive,integer"` Total time.Duration `json:"total" swaggertype:"primitive,integer"`
} }

View File

@ -1,12 +1,14 @@
package v1 package v1
import ( import (
"fmt"
"github.com/gorilla/mux" "github.com/gorilla/mux"
conf "github.com/muety/wakapi/config" conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
v1 "github.com/muety/wakapi/models/compat/shields/v1" v1 "github.com/muety/wakapi/models/compat/shields/v1"
"github.com/muety/wakapi/services" "github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils" "github.com/muety/wakapi/utils"
"github.com/patrickmn/go-cache"
"net/http" "net/http"
"regexp" "regexp"
"time" "time"
@ -21,12 +23,14 @@ type BadgeHandler struct {
config *conf.Config config *conf.Config
userSrvc services.IUserService userSrvc services.IUserService
summarySrvc services.ISummaryService summarySrvc services.ISummaryService
cache *cache.Cache
} }
func NewBadgeHandler(summaryService services.ISummaryService, userService services.IUserService) *BadgeHandler { func NewBadgeHandler(summaryService services.ISummaryService, userService services.IUserService) *BadgeHandler {
return &BadgeHandler{ return &BadgeHandler{
summarySrvc: summaryService, summarySrvc: summaryService,
userSrvc: userService, userSrvc: userService,
cache: cache.New(time.Hour, time.Hour),
config: conf.Get(), config: conf.Get(),
} }
} }
@ -95,6 +99,12 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
filters = &models.Filters{} filters = &models.Filters{}
} }
cacheKey := fmt.Sprintf("%s_%v_%s_%s", user.ID, *interval, filterEntity, filterKey)
if cacheResult, ok := h.cache.Get(cacheKey); ok {
utils.RespondJSON(w, http.StatusOK, cacheResult.(*v1.BadgeData))
return
}
summary, err, status := h.loadUserSummary(user, interval) summary, err, status := h.loadUserSummary(user, interval)
if err != nil { if err != nil {
w.WriteHeader(status) w.WriteHeader(status)
@ -103,6 +113,7 @@ func (h *BadgeHandler) Get(w http.ResponseWriter, r *http.Request) {
} }
vm := v1.NewBadgeDataFrom(summary, filters) vm := v1.NewBadgeDataFrom(summary, filters)
h.cache.SetDefault(cacheKey, vm)
utils.RespondJSON(w, http.StatusOK, vm) utils.RespondJSON(w, http.StatusOK, vm)
} }