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

chore: configurable count cache ttl

This commit is contained in:
Ferdinand Mütsch 2021-06-27 12:08:11 +02:00
parent 69f73fc0ea
commit 48513b660d
2 changed files with 8 additions and 8 deletions

View File

@ -66,6 +66,7 @@ type appConfig struct {
ImportBackoffMin int `yaml:"import_backoff_min" default:"5" env:"WAKAPI_IMPORT_BACKOFF_MIN"`
ImportBatchSize int `yaml:"import_batch_size" default:"50" env:"WAKAPI_IMPORT_BATCH_SIZE"`
InactiveDays int `yaml:"inactive_days" default:"7" env:"WAKAPI_INACTIVE_DAYS"`
CountCacheTTLMin int `yaml:"count_cache_ttl_min" default:"30" env:"WAKAPI_COUNT_CACHE_TTL_MIN"`
CustomLanguages map[string]string `yaml:"custom_languages"`
Colors map[string]map[string]string `yaml:"-"`
}

View File

@ -13,11 +13,6 @@ import (
"github.com/muety/wakapi/models"
)
const (
// re-read heartbeat counters from database every 10 min, in between, rely on local state
countersTtl = 10 * time.Minute
)
type HeartbeatService struct {
config *config.Config
cache *cache.Cache
@ -82,7 +77,7 @@ func (srv *HeartbeatService) Count() (int64, error) {
}
count, err := srv.repository.Count()
if err == nil {
srv.cache.Set(srv.countTotalCacheKey(), count, countersTtl)
srv.cache.Set(srv.countTotalCacheKey(), count, srv.countCacheTtl())
}
return count, err
}
@ -95,7 +90,7 @@ func (srv *HeartbeatService) CountByUser(user *models.User) (int64, error) {
}
count, err := srv.repository.CountByUser(user)
if err == nil {
srv.cache.Set(key, count, countersTtl)
srv.cache.Set(key, count, srv.countCacheTtl())
}
return count, err
}
@ -121,7 +116,7 @@ func (srv *HeartbeatService) CountByUsers(users []*models.User) ([]*models.Count
for _, uc := range counts {
key := srv.countByUserCacheKey(uc.User)
srv.cache.Set(key, uc.Count, countersTtl)
srv.cache.Set(key, uc.Count, srv.countCacheTtl())
userCounts = append(userCounts, uc)
}
@ -226,3 +221,7 @@ func (srv *HeartbeatService) countByUserCacheKey(userId string) string {
func (srv *HeartbeatService) countTotalCacheKey() string {
return "heartbeat-count"
}
func (srv *HeartbeatService) countCacheTtl() time.Duration {
return time.Duration(srv.config.App.CountCacheTTLMin) * time.Minute
}