parent
467d44adc6
commit
4c931fba5c
@ -0,0 +1,90 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/db/relation"
|
||||||
|
"Open_IM/pkg/common/tracelog"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/dtm-labs/rockscache"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
UserExpireTime = time.Second * 60 * 60 * 12
|
||||||
|
userInfoKey = "USER_INFO:"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserCache struct {
|
||||||
|
userDB *relation.User
|
||||||
|
|
||||||
|
expireTime time.Duration
|
||||||
|
redisClient *RedisClient
|
||||||
|
rcClient *rockscache.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserCache(rdb redis.UniversalClient, userDB *relation.User, options rockscache.Options) *UserCache {
|
||||||
|
return &UserCache{
|
||||||
|
userDB: userDB,
|
||||||
|
expireTime: UserExpireTime,
|
||||||
|
redisClient: NewRedisClient(rdb),
|
||||||
|
rcClient: rockscache.NewClient(rdb, options),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCache) getUserInfoKey(userID string) string {
|
||||||
|
return userInfoKey + userID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCache) GetUserInfo(ctx context.Context, userID string) (userInfo *relation.User, err error) {
|
||||||
|
getUserInfo := func() (string, error) {
|
||||||
|
userInfo, err := u.userDB.Take(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
bytes, err := json.Marshal(userInfo)
|
||||||
|
if err != nil {
|
||||||
|
return "", utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
return string(bytes), nil
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "userInfo", *userInfo)
|
||||||
|
}()
|
||||||
|
userInfoStr, err := u.rcClient.Fetch(u.getUserInfoKey(userID), time.Second*30*60, getUserInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
userInfo = &relation.User{}
|
||||||
|
err = json.Unmarshal([]byte(userInfoStr), userInfo)
|
||||||
|
return userInfo, utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCache) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relation.User, error) {
|
||||||
|
var users []*relation.User
|
||||||
|
for _, userID := range userIDs {
|
||||||
|
user, err := GetUserInfoFromCache(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
users = append(users, user)
|
||||||
|
}
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCache) DelUserInfo(ctx context.Context, userID string) (err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
|
||||||
|
}()
|
||||||
|
return u.rcClient.TagAsDeleted(u.getUserInfoKey(userID) + userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserCache) DelUsersInfo(ctx context.Context, userIDs []string) (err error) {
|
||||||
|
for _, userID := range userIDs {
|
||||||
|
if err := u.DelUserInfo(ctx, userID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/db/relation"
|
||||||
|
pbMsg "Open_IM/pkg/proto/msg"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ChatLogInterface interface {
|
||||||
|
CreateChatLog(msg pbMsg.MsgDataToMQ) error
|
||||||
|
GetChatLog(chatLog *relation.ChatLog, pageNumber, showNumber int32, contentTypeList []int32) (int64, []relation.ChatLog, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewChatLogController(db *gorm.DB) ChatLogInterface {
|
||||||
|
return &ChatLogController{database: NewChatLogDataBase(db)}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChatLogController struct {
|
||||||
|
database ChatLogDataBaseInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ChatLogController) CreateChatLog(msg pbMsg.MsgDataToMQ) error {
|
||||||
|
return c.database.CreateChatLog(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ChatLogController) GetChatLog(chatLog *relation.ChatLog, pageNumber, showNumber int32, contentTypeList []int32) (int64, []relation.ChatLog, error) {
|
||||||
|
return c.database.GetChatLog(chatLog, pageNumber, showNumber, contentTypeList)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChatLogDataBaseInterface interface {
|
||||||
|
CreateChatLog(msg pbMsg.MsgDataToMQ) error
|
||||||
|
GetChatLog(chatLog *relation.ChatLog, pageNumber, showNumber int32, contentTypeList []int32) (int64, []relation.ChatLog, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChatLogDataBase struct {
|
||||||
|
chatLogDB *relation.ChatLog
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewChatLogDataBase(db *gorm.DB) ChatLogDataBaseInterface {
|
||||||
|
return &ChatLogDataBase{chatLogDB: relation.NewChatLog(db)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ChatLogDataBase) CreateChatLog(msg pbMsg.MsgDataToMQ) error {
|
||||||
|
return c.chatLogDB.Create(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ChatLogDataBase) GetChatLog(chatLog *relation.ChatLog, pageNumber, showNumber int32, contentTypeList []int32) (int64, []relation.ChatLog, error) {
|
||||||
|
return c.chatLogDB.GetChatLog(chatLog, pageNumber, showNumber, contentTypeList)
|
||||||
|
}
|
@ -1,50 +0,0 @@
|
|||||||
package relation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var AppDB *gorm.DB
|
|
||||||
|
|
||||||
type AppVersion struct {
|
|
||||||
Version string `gorm:"column:version;size:64" json:"version"`
|
|
||||||
Type int `gorm:"column:type;primary_key" json:"type"`
|
|
||||||
UpdateTime int `gorm:"column:update_time" json:"update_time"`
|
|
||||||
ForceUpdate bool `gorm:"column:force_update" json:"force_update"`
|
|
||||||
FileName string `gorm:"column:file_name" json:"file_name"`
|
|
||||||
YamlName string `gorm:"column:yaml_name" json:"yaml_name"`
|
|
||||||
UpdateLog string `gorm:"column:update_log" json:"update_log"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (AppVersion) TableName() string {
|
|
||||||
return "app_version"
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateAppVersion(appType int, version string, forceUpdate bool, fileName, yamlName, updateLog string) error {
|
|
||||||
updateTime := int(time.Now().Unix())
|
|
||||||
app := AppVersion{
|
|
||||||
Version: version,
|
|
||||||
Type: appType,
|
|
||||||
UpdateTime: updateTime,
|
|
||||||
FileName: fileName,
|
|
||||||
YamlName: yamlName,
|
|
||||||
ForceUpdate: forceUpdate,
|
|
||||||
UpdateLog: updateLog,
|
|
||||||
}
|
|
||||||
result := AppDB.Model(AppVersion{}).Where("type = ?", appType).Updates(map[string]interface{}{"force_update": forceUpdate,
|
|
||||||
"version": version, "update_time": int(time.Now().Unix()), "file_name": fileName, "yaml_name": yamlName, "type": appType, "update_log": updateLog})
|
|
||||||
if result.Error != nil {
|
|
||||||
return result.Error
|
|
||||||
}
|
|
||||||
if result.RowsAffected == 0 {
|
|
||||||
err := AppDB.Create(&app).Error
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetNewestVersion(appType int) (*AppVersion, error) {
|
|
||||||
app := AppVersion{}
|
|
||||||
return &app, AppDB.Model(AppVersion{}).First(&app, appType).Error
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"Open_IM/pkg/common/config"
|
|
||||||
rocksCache "Open_IM/pkg/common/db/rocks_cache"
|
|
||||||
"Open_IM/pkg/common/log"
|
|
||||||
"Open_IM/pkg/getcdv3"
|
|
||||||
pbCache "Open_IM/pkg/proto/cache"
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GroupMemberUserIDListHash struct {
|
|
||||||
MemberListHash uint64
|
|
||||||
UserIDList []string
|
|
||||||
}
|
|
||||||
|
|
||||||
var CacheGroupMemberUserIDList = make(map[string]*GroupMemberUserIDListHash, 0)
|
|
||||||
var CacheGroupMtx sync.RWMutex
|
|
||||||
|
|
||||||
func GetGroupMemberUserIDList(ctx context.Context, groupID string, operationID string) ([]string, error) {
|
|
||||||
groupHashRemote, err := GetGroupMemberUserIDListHashFromRemote(ctx, groupID)
|
|
||||||
if err != nil {
|
|
||||||
CacheGroupMtx.Lock()
|
|
||||||
defer CacheGroupMtx.Unlock()
|
|
||||||
delete(CacheGroupMemberUserIDList, groupID)
|
|
||||||
log.Error(operationID, "GetGroupMemberUserIDListHashFromRemote failed ", err.Error(), groupID)
|
|
||||||
return nil, Wrap(err, groupID)
|
|
||||||
}
|
|
||||||
|
|
||||||
CacheGroupMtx.Lock()
|
|
||||||
defer CacheGroupMtx.Unlock()
|
|
||||||
|
|
||||||
if groupHashRemote == 0 {
|
|
||||||
log.Info(operationID, "groupHashRemote == 0 ", groupID)
|
|
||||||
delete(CacheGroupMemberUserIDList, groupID)
|
|
||||||
return []string{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
groupInLocalCache, ok := CacheGroupMemberUserIDList[groupID]
|
|
||||||
if ok && groupInLocalCache.MemberListHash == groupHashRemote {
|
|
||||||
log.Debug(operationID, "in local cache ", groupID)
|
|
||||||
return groupInLocalCache.UserIDList, nil
|
|
||||||
}
|
|
||||||
log.Debug(operationID, "not in local cache or hash changed", groupID, " remote hash ", groupHashRemote, " in cache ", ok)
|
|
||||||
memberUserIDListRemote, err := GetGroupMemberUserIDListFromRemote(groupID, operationID)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(operationID, "GetGroupMemberUserIDListFromRemote failed ", err.Error(), groupID)
|
|
||||||
return nil, Wrap(err, groupID)
|
|
||||||
}
|
|
||||||
CacheGroupMemberUserIDList[groupID] = &GroupMemberUserIDListHash{MemberListHash: groupHashRemote, UserIDList: memberUserIDListRemote}
|
|
||||||
return memberUserIDListRemote, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupMemberUserIDListHashFromRemote(ctx context.Context, groupID string) (uint64, error) {
|
|
||||||
return rocksCache.GetGroupMemberListHashFromCache(ctx, groupID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupMemberUserIDListFromRemote(groupID string, operationID string) ([]string, error) {
|
|
||||||
getGroupMemberIDListFromCacheReq := &pbCache.GetGroupMemberIDListFromCacheReq{OperationID: operationID, GroupID: groupID}
|
|
||||||
etcdConn, err := getcdv3.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImCacheName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
client := pbCache.NewCacheClient(etcdConn)
|
|
||||||
cacheResp, err := client.GetGroupMemberIDListFromCache(context.Background(), getGroupMemberIDListFromCacheReq)
|
|
||||||
if err != nil {
|
|
||||||
log.NewError(operationID, "GetGroupMemberIDListFromCache rpc call failed ", err.Error())
|
|
||||||
return nil, Wrap(err, "GetGroupMemberIDListFromCache rpc call failed")
|
|
||||||
}
|
|
||||||
if cacheResp.CommonResp.ErrCode != 0 {
|
|
||||||
errMsg := operationID + "GetGroupMemberIDListFromCache rpc logic call failed " + cacheResp.CommonResp.ErrMsg
|
|
||||||
log.NewError(operationID, errMsg)
|
|
||||||
return nil, errors.New("errMsg")
|
|
||||||
}
|
|
||||||
return cacheResp.UserIDList, nil
|
|
||||||
}
|
|
Loading…
Reference in new issue