From 8c280e11343739b012c5a86cb2dc656ae6fcff02 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 18 Sep 2023 13:39:50 +0800 Subject: [PATCH] add cache support for get user info --- internal/conf/cache.go | 7 ++++ internal/conf/config.yaml | 1 + internal/conf/setting.go | 1 + internal/dao/cache/common.go | 61 +++++++++++++++++++++++++++++++++++ internal/dao/cache/events.go | 33 +++++++++++++++++++ internal/dao/jinzhu/jinzhu.go | 2 +- 6 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 internal/dao/cache/common.go diff --git a/internal/conf/cache.go b/internal/conf/cache.go index 0acc1be6..9f61a309 100644 --- a/internal/conf/cache.go +++ b/internal/conf/cache.go @@ -26,6 +26,9 @@ const ( PrefixIdxTweetsNewest = "paopao:index:tweets:newest:" PrefixIdxTweetsHots = "paopao:index:tweets:hots:" PrefixIdxTweetsFollowing = "paopao:index:tweets:following:" + PrefixUserInfo = "paopao:userinfo:" + PrefixUserInfoById = "paopao:userinfo:id:" + PrefixUserInfoByName = "paopao:userinfo:name:" KeySiteStatus = "paopao:sitestatus" KeyHistoryMaxOnline = "history.max.online" ) @@ -37,6 +40,8 @@ var ( KeyFollowingTweets cache.KeyPool[string] KeyUnreadMsg cache.KeyPool[int64] KeyOnlineUser cache.KeyPool[int64] + KeyUserInfoById cache.KeyPool[int64] + KeyUserInfoByName cache.KeyPool[string] ) func initCacheKeyPool() { @@ -49,6 +54,8 @@ func initCacheKeyPool() { KeyFollowingTweets = strKeyPool(poolSize, PrefixFollowingTweets) KeyUnreadMsg = intKeyPool[int64](poolSize, PrefixUnreadmsg) KeyOnlineUser = intKeyPool[int64](poolSize, PrefixOnlineUser) + KeyUserInfoById = intKeyPool[int64](poolSize, PrefixUserInfoById) + KeyUserInfoByName = strKeyPool(poolSize, PrefixUserInfoById) } func strKeyPool(size int, prefix string) cache.KeyPool[string] { diff --git a/internal/conf/config.yaml b/internal/conf/config.yaml index 3e3ede5f..7c53f4aa 100644 --- a/internal/conf/config.yaml +++ b/internal/conf/config.yaml @@ -12,6 +12,7 @@ Cache: UserTweetsExpire: 60 # 获取用户推文列表过期时间,单位秒, 默认60s IndexTweetsExpire: 120 # 获取广场推文列表过期时间,单位秒, 默认120s OnlineUserExpire: 300 # 标记在线用户 过期时间,单位秒, 默认300s + UserInfoExpire: 300 # 获取用户信息过期时间,单位秒, 默认300s EventManager: # 事件管理器的配置参数 MinWorker: 64 # 最小后台工作者, 设置范围[5, ++], 默认64 MaxEventBuf: 128 # 最大log缓存条数, 设置范围[10, ++], 默认128 diff --git a/internal/conf/setting.go b/internal/conf/setting.go index 64678feb..51ad246c 100644 --- a/internal/conf/setting.go +++ b/internal/conf/setting.go @@ -103,6 +103,7 @@ type cacheConf struct { UserTweetsExpire int64 IndexTweetsExpire int64 OnlineUserExpire int64 + UserInfoExpire int64 } type eventManagerConf struct { diff --git a/internal/dao/cache/common.go b/internal/dao/cache/common.go new file mode 100644 index 00000000..f01af983 --- /dev/null +++ b/internal/dao/cache/common.go @@ -0,0 +1,61 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package cache + +import ( + "bytes" + "encoding/gob" + + "github.com/rocboss/paopao-ce/internal/conf" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/ms" +) + +type cacheDataService struct { + core.DataService + ac core.AppCache +} + +func NewCacheDataService(ds core.DataService) core.DataService { + lazyInitial() + return &cacheDataService{ + DataService: ds, + ac: _appCache, + } +} + +func (s *cacheDataService) GetUserByID(id int64) (res *ms.User, err error) { + // 先从缓存获取, 不处理错误 + key := conf.KeyUserInfoById.Get(id) + if data, xerr := s.ac.Get(key); xerr == nil { + buf := bytes.NewBuffer(data) + res = &ms.User{} + err = gob.NewDecoder(buf).Decode(res) + return + } + // 最后查库 + if res, err = s.DataService.GetUserByID(id); err == nil { + // 更新缓存 + onCacheUserInfoEvent(key, res) + } + return +} + +func (s *cacheDataService) GetUserByUsername(username string) (res *ms.User, err error) { + // 先从缓存获取, 不处理错误 + key := conf.KeyUserInfoByName.Get(username) + if data, xerr := s.ac.Get(key); xerr == nil { + buf := bytes.NewBuffer(data) + res = &ms.User{} + err = gob.NewDecoder(buf).Decode(res) + return + } + // 最后查库 + if res, err = s.DataService.GetUserByUsername(username); err == nil { + // 更新缓存 + onCacheUserInfoEvent(key, res) + } + return +} diff --git a/internal/dao/cache/events.go b/internal/dao/cache/events.go index 4f137200..17bdbdfb 100644 --- a/internal/dao/cache/events.go +++ b/internal/dao/cache/events.go @@ -5,6 +5,8 @@ package cache import ( + "bytes" + "encoding/gob" "fmt" "github.com/alimy/tryst/event" @@ -35,6 +37,15 @@ type expireFollowTweetsEvent struct { keyPattern string } +type cacheUserInfoEvent struct { + event.UnimplementedEvent + tweet *ms.Post + ac core.AppCache + key string + data *ms.User + expire int64 +} + func onExpireIndexTweetEvent(tweet *ms.Post) { events.OnEvent(&expireIndexTweetsEvent{ tweet: tweet, @@ -63,6 +74,15 @@ func onExpireFollowTweetEvent(tweet *ms.Post) { }) } +func onCacheUserInfoEvent(key string, data *ms.User) { + events.OnEvent(&cacheUserInfoEvent{ + key: key, + data: data, + ac: _appCache, + expire: conf.CacheSetting.UserInfoExpire, + }) +} + func (e *expireIndexTweetsEvent) Name() string { return "expireIndexTweetsEvent" } @@ -94,3 +114,16 @@ func (e *expireFollowTweetsEvent) Action() (err error) { e.ac.DelAny(e.keyPattern) return } + +func (e *cacheUserInfoEvent) Name() string { + return "cacheUserInfoEvent" +} + +func (e *cacheUserInfoEvent) Action() (err error) { + buffer := &bytes.Buffer{} + ge := gob.NewEncoder(buffer) + if err = ge.Encode(e.data); err == nil { + e.ac.Set(e.key, buffer.Bytes(), e.expire) + } + return +} diff --git a/internal/dao/jinzhu/jinzhu.go b/internal/dao/jinzhu/jinzhu.go index 0645bd5b..7847b8e9 100644 --- a/internal/dao/jinzhu/jinzhu.go +++ b/internal/dao/jinzhu/jinzhu.go @@ -77,7 +77,7 @@ func NewDataService() (core.DataService, core.VersionInfo) { SecurityService: newSecurityService(db, pvs), AttachmentCheckService: security.NewAttachmentCheckService(), } - return ds, ds + return cache.NewCacheDataService(ds), ds } func NewWebDataServantA() (core.WebDataServantA, core.VersionInfo) {