From 7e425ae99804872b4a77973d09d268c916ab2ef8 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 6 Oct 2023 21:54:51 +0800 Subject: [PATCH] optimize cache logic for change user nickname or avatar info --- internal/conf/cache.go | 2 +- internal/dao/cache/events.go | 40 +++++++++++++++++++++++++++++++++ internal/servants/web/core.go | 4 ++++ internal/servants/web/events.go | 23 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/internal/conf/cache.go b/internal/conf/cache.go index 25a745f6..27652c5f 100644 --- a/internal/conf/cache.go +++ b/internal/conf/cache.go @@ -68,7 +68,7 @@ func initCacheKeyPool() { KeyUnreadMsg = intKeyPool[int64](poolSize, PrefixUnreadmsg) KeyOnlineUser = intKeyPool[int64](poolSize, PrefixOnlineUser) KeyUserInfoById = intKeyPool[int64](poolSize, PrefixUserInfoById) - KeyUserInfoByName = strKeyPool(poolSize, PrefixUserInfoById) + KeyUserInfoByName = strKeyPool(poolSize, PrefixUserInfoByName) KeyUserProfileByName = strKeyPool(poolSize, prefixUserProfileByName) KeyMyFriendIds = intKeyPool[int64](poolSize, PrefixMyFriendIds) KeyMyFollowIds = intKeyPool[int64](poolSize, PrefixMyFollowIds) diff --git a/internal/dao/cache/events.go b/internal/dao/cache/events.go index 2f847ed7..e8c0e62b 100644 --- a/internal/dao/cache/events.go +++ b/internal/dao/cache/events.go @@ -18,6 +18,11 @@ import ( "github.com/sirupsen/logrus" ) +type BaseCacheEvent struct { + event.UnimplementedEvent + ac core.AppCache +} + type expireIndexTweetsEvent struct { event.UnimplementedEvent ac core.AppCache @@ -70,6 +75,12 @@ type cacheMyFollowIdsEvent struct { expire int64 } +func NewBaseCacheEvent(ac core.AppCache) *BaseCacheEvent { + return &BaseCacheEvent{ + ac: ac, + } +} + func OnExpireIndexTweetEvent(userId int64) { // TODO: 这里暴躁的将所有 最新/热门/关注 的推文列表缓存都过期掉,后续需要更精细话处理 events.OnEvent(&expireIndexTweetsEvent{ @@ -144,6 +155,35 @@ func OnCacheMyFollowIdsEvent(urs core.UserRelationService, userId int64, key ... }) } +func (e *BaseCacheEvent) ExpireUserInfo(id int64, name string) error { + keys := make([]string, 0, 2) + if id >= 0 { + keys = append(keys, conf.KeyUserInfoById.Get(id)) + } + if len(name) > 0 { + keys = append(keys, conf.KeyUserInfoByName.Get(name)) + } + return e.ac.Delete(keys...) +} + +func (e *BaseCacheEvent) ExpireUserProfile(name string) error { + if len(name) > 0 { + return e.ac.Delete(conf.KeyUserProfileByName.Get(name)) + } + return nil +} + +func (e *BaseCacheEvent) ExpireUserData(id int64, name string) error { + keys := make([]string, 0, 3) + if id >= 0 { + keys = append(keys, conf.KeyUserInfoById.Get(id)) + } + if len(name) > 0 { + keys = append(keys, conf.KeyUserInfoByName.Get(name), conf.KeyUserProfileByName.Get(name)) + } + return e.ac.Delete(keys...) +} + func (e *expireIndexTweetsEvent) Name() string { return "expireIndexTweetsEvent" } diff --git a/internal/servants/web/core.go b/internal/servants/web/core.go index 46d276dc..4430f6ee 100644 --- a/internal/servants/web/core.go +++ b/internal/servants/web/core.go @@ -353,6 +353,8 @@ func (s *coreSrv) ChangeNickname(req *web.ChangeNicknameReq) mir.Error { logrus.Errorf("Ds.UpdateUser err: %s", err) return xerror.ServerError } + // 缓存处理 + onChangeUsernameEvent(user.ID, user.Username) return nil } @@ -377,6 +379,8 @@ func (s *coreSrv) ChangeAvatar(req *web.ChangeAvatarReq) (xerr mir.Error) { logrus.Errorf("Ds.UpdateUser failed: %s", err) return xerror.ServerError } + // 缓存处理 + onChangeUsernameEvent(user.ID, user.Username) return nil } diff --git a/internal/servants/web/events.go b/internal/servants/web/events.go index 91d117c7..79c23598 100644 --- a/internal/servants/web/events.go +++ b/internal/servants/web/events.go @@ -13,6 +13,7 @@ import ( "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/core/cs" "github.com/rocboss/paopao-ce/internal/core/ms" + "github.com/rocboss/paopao-ce/internal/dao/cache" "github.com/rocboss/paopao-ce/internal/events" "github.com/rocboss/paopao-ce/internal/model/joint" "github.com/rocboss/paopao-ce/internal/model/web" @@ -98,6 +99,20 @@ type trendsActionEvent struct { userIds []int64 } +type changeUserEvent struct { + *cache.BaseCacheEvent + userId int64 + username string +} + +func onChangeUsernameEvent(id int64, name string) { + events.OnEvent(&changeUserEvent{ + BaseCacheEvent: cache.NewBaseCacheEvent(_ac), + userId: id, + username: name, + }) +} + func onTrendsActionEvent(action uint8, userIds ...int64) { events.OnEvent(&trendsActionEvent{ ac: _ac, @@ -322,3 +337,11 @@ func (e *tweetActionEvent) Action() (err error) { } return } + +func (e *changeUserEvent) Name() string { + return "changeUserEvent" +} + +func (e *changeUserEvent) Action() error { + return e.ExpireUserData(e.userId, e.username) +}