add cache support for get user info

pull/391/head
Michael Li 1 year ago
parent 2a44f94f3e
commit 8c280e1134
No known key found for this signature in database

@ -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] {

@ -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

@ -103,6 +103,7 @@ type cacheConf struct {
UserTweetsExpire int64
IndexTweetsExpire int64
OnlineUserExpire int64
UserInfoExpire int64
}
type eventManagerConf struct {

@ -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
}

@ -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
}

@ -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) {

Loading…
Cancel
Save