From 601c60fca786ca2342ce4e42b40feb0d1d75ddc0 Mon Sep 17 00:00:00 2001 From: lishuang Date: Sat, 20 Jun 2020 22:17:01 +0800 Subject: [PATCH] Finish the cache refresh things. --- code/rest/user_controller.go | 10 +++++----- code/rest/user_service.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/code/rest/user_controller.go b/code/rest/user_controller.go index be6907a..6a440bf 100644 --- a/code/rest/user_controller.go +++ b/code/rest/user_controller.go @@ -287,6 +287,9 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque currentUser = this.userDao.Save(currentUser) + //remove cache user. + this.userService.RemoveCacheUserByUuid(currentUser.Uuid) + return this.Success(currentUser) } @@ -400,11 +403,8 @@ func (this *UserController) ToggleStatus(writer http.ResponseWriter, request *ht currentUser = this.userDao.Save(currentUser) - cacheUsers := this.userService.FindCacheUsersByUuid(currentUser.Uuid) - this.logger.Info("find %d cache users", len(cacheUsers)) - for _, u := range cacheUsers { - u.Status = currentUser.Status - } + //remove cache user. + this.userService.RemoveCacheUserByUuid(currentUser.Uuid) return this.Success(currentUser) diff --git a/code/rest/user_service.go b/code/rest/user_service.go index f11e5c1..00f8556 100644 --- a/code/rest/user_service.go +++ b/code/rest/user_service.go @@ -158,3 +158,32 @@ func (this *UserService) FindCacheUsersByUuid(userUuid string) []*User { return users } + +//remove cache user by its userUuid +func (this *UserService) RemoveCacheUserByUuid(userUuid string) { + + var sessionId interface{} + //let session user work. + core.CONTEXT.GetSessionCache().Foreach(func(key interface{}, cacheItem *cache.Item) { + if cacheItem == nil || cacheItem.Data() == nil { + return + } + if value, ok := cacheItem.Data().(*User); ok { + var user = value + if user.Uuid == userUuid { + sessionId = key + this.logger.Info("sessionId %v", key) + } + } else { + this.logger.Error("cache item not store the *User") + } + }) + + exists := core.CONTEXT.GetSessionCache().Exists(sessionId) + if exists { + _, err := core.CONTEXT.GetSessionCache().Delete(sessionId) + if err != nil { + this.logger.Error("occur error when deleting cache user.") + } + } +}