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

fix: invalidate user summary cache (fix #209)

This commit is contained in:
Ferdinand Mütsch 2021-06-12 10:43:56 +02:00
parent 628ea0b9dd
commit 3780ae4255

View File

@ -1,7 +1,6 @@
package services package services
import ( import (
"crypto/md5"
"errors" "errors"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/muety/wakapi/config" "github.com/muety/wakapi/config"
@ -10,6 +9,7 @@ import (
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
"math" "math"
"sort" "sort"
"strings"
"time" "time"
) )
@ -173,10 +173,12 @@ func (srv *SummaryService) GetLatestByUser() ([]*models.TimeByUser, error) {
} }
func (srv *SummaryService) DeleteByUser(userId string) error { func (srv *SummaryService) DeleteByUser(userId string) error {
srv.invalidateUserCache(userId)
return srv.repository.DeleteByUser(userId) return srv.repository.DeleteByUser(userId)
} }
func (srv *SummaryService) Insert(summary *models.Summary) error { func (srv *SummaryService) Insert(summary *models.Summary) error {
srv.invalidateUserCache(summary.UserID)
return srv.repository.Insert(summary) return srv.repository.Insert(summary)
} }
@ -396,9 +398,13 @@ func (srv *SummaryService) getMissingIntervals(from, to time.Time, summaries []*
} }
func (srv *SummaryService) getHash(args ...string) string { func (srv *SummaryService) getHash(args ...string) string {
digest := md5.New() return strings.Join(args, "__")
for _, a := range args { }
digest.Write([]byte(a))
} func (srv *SummaryService) invalidateUserCache(userId string) {
return string(digest.Sum(nil)) for key := range srv.cache.Items() {
if strings.Contains(key, userId) {
srv.cache.Delete(key)
}
}
} }