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

fix: summary caching (resolve #211)

This commit is contained in:
Ferdinand Mütsch 2021-06-19 12:47:35 +02:00
parent 82b8951437
commit 466f2e1786
3 changed files with 5 additions and 14 deletions

View File

@ -121,17 +121,16 @@ func (h *SummariesHandler) loadUserSummaries(r *http.Request) ([]*models.Summary
end = utils.EndOfDay(end).Add(-1 * time.Second) end = utils.EndOfDay(end).Add(-1 * time.Second)
overallParams := &models.SummaryParams{ overallParams := &models.SummaryParams{
From: start, From: start,
To: end, To: end,
User: user, User: user,
Recompute: false,
} }
intervals := utils.SplitRangeByDays(overallParams.From, overallParams.To) intervals := utils.SplitRangeByDays(overallParams.From, overallParams.To)
summaries := make([]*models.Summary, len(intervals)) summaries := make([]*models.Summary, len(intervals))
for i, interval := range intervals { for i, interval := range intervals {
summary, err := h.summarySrvc.Aliased(interval[0], interval[1], user, h.summarySrvc.Retrieve, false) summary, err := h.summarySrvc.Aliased(interval[0], interval[1], user, h.summarySrvc.Retrieve, end.After(time.Now()))
if err != nil { if err != nil {
return nil, err, http.StatusInternalServerError return nil, err, http.StatusInternalServerError
} }

View File

@ -73,12 +73,6 @@ func (srv *SummaryService) Aliased(from, to time.Time, user *models.User, f Summ
} }
func (srv *SummaryService) Retrieve(from, to time.Time, user *models.User) (*models.Summary, error) { func (srv *SummaryService) Retrieve(from, to time.Time, user *models.User) (*models.Summary, error) {
// Check cache
cacheKey := srv.getHash(from.String(), to.String(), user.ID)
if cacheResult, ok := srv.cache.Get(cacheKey); ok {
return cacheResult.(*models.Summary), nil
}
// Get all already existing, pre-generated summaries that fall into the requested interval // Get all already existing, pre-generated summaries that fall into the requested interval
summaries, err := srv.repository.GetByUserWithin(user, from, to) summaries, err := srv.repository.GetByUserWithin(user, from, to)
if err != nil { if err != nil {
@ -101,8 +95,6 @@ func (srv *SummaryService) Retrieve(from, to time.Time, user *models.User) (*mod
return nil, err return nil, err
} }
// Cache 'em
srv.cache.SetDefault(cacheKey, summary)
return summary.Sorted(), nil return summary.Sorted(), nil
} }

View File

@ -1 +1 @@
1.28.1 1.28.2