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:
parent
69f73fc0ea
commit
48513b660d
@ -66,6 +66,7 @@ type appConfig struct {
|
|||||||
ImportBackoffMin int `yaml:"import_backoff_min" default:"5" env:"WAKAPI_IMPORT_BACKOFF_MIN"`
|
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"`
|
ImportBatchSize int `yaml:"import_batch_size" default:"50" env:"WAKAPI_IMPORT_BATCH_SIZE"`
|
||||||
InactiveDays int `yaml:"inactive_days" default:"7" env:"WAKAPI_INACTIVE_DAYS"`
|
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"`
|
CustomLanguages map[string]string `yaml:"custom_languages"`
|
||||||
Colors map[string]map[string]string `yaml:"-"`
|
Colors map[string]map[string]string `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,6 @@ import (
|
|||||||
"github.com/muety/wakapi/models"
|
"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 {
|
type HeartbeatService struct {
|
||||||
config *config.Config
|
config *config.Config
|
||||||
cache *cache.Cache
|
cache *cache.Cache
|
||||||
@ -82,7 +77,7 @@ func (srv *HeartbeatService) Count() (int64, error) {
|
|||||||
}
|
}
|
||||||
count, err := srv.repository.Count()
|
count, err := srv.repository.Count()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
srv.cache.Set(srv.countTotalCacheKey(), count, countersTtl)
|
srv.cache.Set(srv.countTotalCacheKey(), count, srv.countCacheTtl())
|
||||||
}
|
}
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
@ -95,7 +90,7 @@ func (srv *HeartbeatService) CountByUser(user *models.User) (int64, error) {
|
|||||||
}
|
}
|
||||||
count, err := srv.repository.CountByUser(user)
|
count, err := srv.repository.CountByUser(user)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
srv.cache.Set(key, count, countersTtl)
|
srv.cache.Set(key, count, srv.countCacheTtl())
|
||||||
}
|
}
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
@ -121,7 +116,7 @@ func (srv *HeartbeatService) CountByUsers(users []*models.User) ([]*models.Count
|
|||||||
|
|
||||||
for _, uc := range counts {
|
for _, uc := range counts {
|
||||||
key := srv.countByUserCacheKey(uc.User)
|
key := srv.countByUserCacheKey(uc.User)
|
||||||
srv.cache.Set(key, uc.Count, countersTtl)
|
srv.cache.Set(key, uc.Count, srv.countCacheTtl())
|
||||||
userCounts = append(userCounts, uc)
|
userCounts = append(userCounts, uc)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,3 +221,7 @@ func (srv *HeartbeatService) countByUserCacheKey(userId string) string {
|
|||||||
func (srv *HeartbeatService) countTotalCacheKey() string {
|
func (srv *HeartbeatService) countTotalCacheKey() string {
|
||||||
return "heartbeat-count"
|
return "heartbeat-count"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *HeartbeatService) countCacheTtl() time.Duration {
|
||||||
|
return time.Duration(srv.config.App.CountCacheTTLMin) * time.Minute
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user