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

chore: clear user cache upon logout

This commit is contained in:
Ferdinand Mütsch
2023-01-02 14:53:21 +01:00
parent fb5b2f52c7
commit ef5b49ebd8
6 changed files with 763 additions and 742 deletions

View File

@ -141,4 +141,5 @@ type IUserService interface {
MigrateMd5Password(*models.User, *models.Login) (*models.User, error)
GenerateResetToken(*models.User) (*models.User, error)
FlushCache()
FlushUserCache(string)
}

View File

@ -67,7 +67,7 @@ func (srv *UserService) GetUserById(userId string) (*models.User, error) {
return nil, err
}
srv.cache.Set(u.ID, u, cache.DefaultExpiration)
srv.cache.SetDefault(u.ID, u)
return u, nil
}
@ -167,19 +167,19 @@ func (srv *UserService) CreateOrGet(signup *models.Signup, isAdmin bool) (*model
}
func (srv *UserService) Update(user *models.User) (*models.User, error) {
srv.cache.Flush()
srv.FlushUserCache(user.ID)
srv.notifyUpdate(user)
return srv.repository.Update(user)
}
func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) {
srv.cache.Flush()
srv.FlushUserCache(user.ID)
user.ApiKey = uuid.NewV4().String()
return srv.Update(user)
}
func (srv *UserService) SetWakatimeApiCredentials(user *models.User, apiKey string, apiUrl string) (*models.User, error) {
srv.cache.Flush()
srv.FlushUserCache(user.ID)
if apiKey != user.WakatimeApiKey {
if u, err := srv.repository.UpdateField(user, "wakatime_api_key", apiKey); err != nil {
@ -195,7 +195,7 @@ func (srv *UserService) SetWakatimeApiCredentials(user *models.User, apiKey stri
}
func (srv *UserService) MigrateMd5Password(user *models.User, login *models.Login) (*models.User, error) {
srv.cache.Flush()
srv.FlushUserCache(user.ID)
user.Password = login.Password
if hash, err := utils.HashBcrypt(user.Password, srv.config.Security.PasswordSalt); err != nil {
return nil, err
@ -210,7 +210,7 @@ func (srv *UserService) GenerateResetToken(user *models.User) (*models.User, err
}
func (srv *UserService) Delete(user *models.User) error {
srv.cache.Flush()
srv.FlushUserCache(user.ID)
user.ReportsWeekly = false
srv.notifyUpdate(user)
@ -222,6 +222,10 @@ func (srv *UserService) FlushCache() {
srv.cache.Flush()
}
func (srv *UserService) FlushUserCache(userId string) {
srv.cache.Delete(userId)
}
func (srv *UserService) notifyUpdate(user *models.User) {
srv.eventBus.Publish(hub.Message{
Name: config.EventUserUpdate,