fix: speed up settings page (resolve #226)

This commit is contained in:
Ferdinand Mütsch 2021-08-06 16:36:01 +02:00
parent 0af5fab75f
commit 533b5d62fc
5 changed files with 495 additions and 482 deletions

File diff suppressed because it is too large Load Diff

11
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"embed"
"github.com/muety/wakapi/models"
"io/fs"
"log"
"net"
@ -237,6 +238,16 @@ func main() {
middlewares.NewFileTypeFilterMiddleware([]string{".go"})(fileServer),
)
// Miscellaneous
// Pre-warm projects cache
allUsers, err := userService.GetAll()
if err == nil {
logbuch.Info("pre-warming user project cache")
for _, u := range allUsers {
go heartbeatService.GetEntitySetByUser(models.SummaryProject, u)
}
}
// Listen HTTP
listen(router)
}

View File

@ -147,7 +147,7 @@ func (h *SettingsHandler) dispatchAction(action string) action {
case "toggle_wakatime":
return h.actionSetWakatimeApiKey
case "import_wakatime":
return h.actionImportWaktime
return h.actionImportWakatime
case "regenerate_summaries":
return h.actionRegenerateSummaries
case "delete_account":
@ -447,7 +447,7 @@ func (h *SettingsHandler) actionSetWakatimeApiKey(w http.ResponseWriter, r *http
return http.StatusOK, "Wakatime API Key updated successfully", ""
}
func (h *SettingsHandler) actionImportWaktime(w http.ResponseWriter, r *http.Request) (int, string, string) {
func (h *SettingsHandler) actionImportWakatime(w http.ResponseWriter, r *http.Request) (int, string, string) {
if h.config.IsDev() {
loadTemplates()
}

View File

@ -16,6 +16,7 @@ import (
type HeartbeatService struct {
config *config.Config
cache *cache.Cache
cache2 *cache.Cache
eventBus *hub.Hub
repository repositories.IHeartbeatRepository
languageMappingSrvc ILanguageMappingService
@ -25,6 +26,7 @@ func NewHeartbeatService(heartbeatRepo repositories.IHeartbeatRepository, langua
srv := &HeartbeatService{
config: config.Get(),
cache: cache.New(24*time.Hour, 24*time.Hour),
cache2: cache.New(cache.NoExpiration, cache.NoExpiration),
eventBus: config.EventBus(),
repository: heartbeatRepo,
languageMappingSrvc: languageMappingService,
@ -145,7 +147,7 @@ func (srv *HeartbeatService) GetFirstByUsers() ([]*models.TimeByUser, error) {
func (srv *HeartbeatService) GetEntitySetByUser(entityType uint8, user *models.User) ([]string, error) {
cacheKey := srv.getEntityUserCacheKey(entityType, user)
if results, found := srv.cache.Get(cacheKey); found {
if results, found := srv.cache2.Get(cacheKey); found {
return utils.SetToStrings(results.(map[string]bool)), nil
}
@ -161,7 +163,7 @@ func (srv *HeartbeatService) GetEntitySetByUser(entityType uint8, user *models.U
}
}
srv.cache.Set(cacheKey, utils.StringsToSet(filtered), cache.DefaultExpiration)
srv.cache2.Set(cacheKey, utils.StringsToSet(filtered), cache.DefaultExpiration)
return filtered, nil
}
@ -188,11 +190,11 @@ func (srv *HeartbeatService) getEntityUserCacheKey(entityType uint8, user *model
func (srv *HeartbeatService) updateEntityUserCache(entityType uint8, entityKey string, user *models.User) {
cacheKey := srv.getEntityUserCacheKey(entityType, user)
if entities, found := srv.cache.Get(cacheKey); found {
if entities, found := srv.cache2.Get(cacheKey); found {
if _, ok := entities.(map[string]bool)[entityKey]; !ok {
// new project / language / ..., which is not yet present in cache, arrived as part of a heartbeats
// -> invalidate cache
srv.cache.Delete(cacheKey)
srv.cache2.Delete(cacheKey)
}
}
}

View File

@ -1 +1 @@
1.29.2
1.29.3