You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/redisModel.go

316 lines
9.9 KiB

package db
import (
3 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
log2 "Open_IM/pkg/common/log"
3 years ago
pbChat "Open_IM/pkg/proto/chat"
3 years ago
pbCommon "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
3 years ago
"encoding/json"
3 years ago
"errors"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/golang/protobuf/jsonpb"
3 years ago
"strconv"
)
const (
accountTempCode = "ACCOUNT_TEMP_CODE"
3 years ago
resetPwdTempCode = "RESET_PWD_TEMP_CODE"
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
appleDeviceToken = "DEVICE_TOKEN"
userMinSeq = "REDIS_USER_MIN_SEQ:"
uidPidToken = "UID_PID_TOKEN_STATUS:"
conversationReceiveMessageOpt = "CON_RECV_MSG_OPT:"
getuiToken = "GETUI"
userInfoCache = "USER_INFO_CACHE:"
friendRelationCache = "FRIEND_RELATION_CACHE:"
blackListCache = "BLACK_LIST_CACHE:"
3 years ago
groupCache = "GROUP_CACHE:"
3 years ago
messageCache = "MESSAGE_CACHE:"
)
3 years ago
func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (interface{}, error) {
con := d.redisPool.Get()
if err := con.Err(); err != nil {
log2.Error("", "", "redis cmd = %v, err = %v", cmd, err)
return nil, err
}
defer con.Close()
params := make([]interface{}, 0)
params = append(params, key)
if len(args) > 0 {
for _, v := range args {
params = append(params, v)
}
}
return con.Do(cmd, params...)
}
3 years ago
func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) {
key := accountTempCode + account
3 years ago
return redis.Bool(d.Exec("EXISTS", key))
}
func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) {
key := accountTempCode + account
3 years ago
_, err = d.Exec("SET", key, code, "ex", ttl)
3 years ago
return err
}
func (d *DataBases) GetAccountCode(account string) (string, error) {
key := accountTempCode + account
3 years ago
return redis.String(d.Exec("GET", key))
}
3 years ago
//Perform seq auto-increment operation of user messages
3 years ago
func (d *DataBases) IncrUserSeq(uid string) (uint64, error) {
key := userIncrSeq + uid
3 years ago
return redis.Uint64(d.Exec("INCR", key))
}
//Get the largest Seq
3 years ago
func (d *DataBases) GetUserMaxSeq(uid string) (uint64, error) {
key := userIncrSeq + uid
3 years ago
return redis.Uint64(d.Exec("GET", key))
}
3 years ago
//set the largest Seq
3 years ago
func (d *DataBases) SetUserMaxSeq(uid string, maxSeq uint64) error {
3 years ago
key := userIncrSeq + uid
3 years ago
_, err := d.Exec("SET", key, maxSeq)
3 years ago
return err
}
//Set the user's minimum seq
3 years ago
func (d *DataBases) SetUserMinSeq(uid string, minSeq uint32) (err error) {
key := userMinSeq + uid
_, err = d.Exec("SET", key, minSeq)
return err
}
//Get the smallest Seq
3 years ago
func (d *DataBases) GetUserMinSeq(uid string) (uint64, error) {
key := userMinSeq + uid
3 years ago
return redis.Uint64(d.Exec("GET", key))
}
//Store Apple's device token to redis
func (d *DataBases) SetAppleDeviceToken(accountAddress, value string) (err error) {
key := appleDeviceToken + accountAddress
3 years ago
_, err = d.Exec("SET", key, value)
return err
}
//Delete Apple device token
func (d *DataBases) DelAppleDeviceToken(accountAddress string) (err error) {
key := appleDeviceToken + accountAddress
3 years ago
_, err = d.Exec("DEL", key)
return err
}
//Store userid and platform class to redis
func (d *DataBases) AddTokenFlag(userID string, platformID int32, token string, flag int) error {
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
3 years ago
log2.NewDebug("", "add token key is ", key)
_, err1 := d.Exec("HSet", key, token, flag)
return err1
}
3 years ago
func (d *DataBases) GetTokenMapByUidPid(userID, platformID string) (map[string]int, error) {
key := uidPidToken + userID + ":" + platformID
3 years ago
log2.NewDebug("", "get token key is ", key)
return redis.IntMap(d.Exec("HGETALL", key))
}
func (d *DataBases) SetTokenMapByUidPid(userID string, platformID int32, m map[string]int) error {
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
3 years ago
_, err := d.Exec("hmset", key, redis.Args{}.Add().AddFlat(m)...)
return err
}
func (d *DataBases) DeleteTokenByUidPid(userID string, platformID int32, fields []string) error {
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
_, err := d.Exec("HDEL", key, redis.Args{}.Add().AddFlat(fields)...)
return err
}
3 years ago
func (d *DataBases) SetSingleConversationRecvMsgOpt(userID, conversationID string, opt int32) error {
key := conversationReceiveMessageOpt + userID
_, err := d.Exec("HSet", key, conversationID, opt)
return err
}
3 years ago
func (d *DataBases) GetSingleConversationRecvMsgOpt(userID, conversationID string) (int, error) {
key := conversationReceiveMessageOpt + userID
return redis.Int(d.Exec("HGet", key, conversationID))
}
func (d *DataBases) GetAllConversationMsgOpt(userID string) (map[string]int, error) {
key := conversationReceiveMessageOpt + userID
return redis.IntMap(d.Exec("HGETALL", key))
}
func (d *DataBases) SetMultiConversationMsgOpt(userID string, m map[string]int) error {
key := conversationReceiveMessageOpt + userID
_, err := d.Exec("hmset", key, redis.Args{}.Add().AddFlat(m)...)
return err
}
3 years ago
func (d *DataBases) GetMultiConversationMsgOpt(userID string, conversationIDs []string) (m map[string]int, err error) {
m = make(map[string]int)
key := conversationReceiveMessageOpt + userID
3 years ago
i, err := redis.Ints(d.Exec("hmget", key, redis.Args{}.Add().AddFlat(conversationIDs)...))
if err != nil {
return m, err
}
for k, v := range conversationIDs {
m[v] = i[k]
}
return m, nil
}
3 years ago
func (d *DataBases) SetGetuiToken(token string, expireTime int64) error {
_, err := d.Exec("SET", getuiToken, token, "ex", expireTime)
3 years ago
return err
}
func (d *DataBases) GetGetuiToken() (string, error) {
result, err := redis.String(d.Exec("GET", getuiToken))
3 years ago
return result, err
}
3 years ago
func (d *DataBases) SearchContentType() {
}
3 years ago
func (d *DataBases) SetUserInfoToCache(userID string, m map[string]interface{}) error {
_, err := d.Exec("hmset", userInfoCache+userID, redis.Args{}.Add().AddFlat(m)...)
3 years ago
return err
}
func (d *DataBases) GetUserInfoFromCache(userID string) (*pbCommon.UserInfo, error) {
result, err := redis.String(d.Exec("hgetall", userInfoCache+userID))
log2.NewInfo("", result)
3 years ago
if err != nil {
return nil, err
}
userInfo := &pbCommon.UserInfo{}
err = json.Unmarshal([]byte(result), userInfo)
return userInfo, err
}
3 years ago
func (d *DataBases) AddFriendToCache(userID string, friendIDList ...string) error {
3 years ago
var IDList []interface{}
for _, id := range friendIDList {
IDList = append(IDList, id)
}
_, err := d.Exec("SADD", friendRelationCache+userID, IDList...)
return err
}
3 years ago
func (d *DataBases) ReduceFriendToCache(userID string, friendIDList ...string) error {
var IDList []interface{}
for _, id := range friendIDList {
IDList = append(IDList, id)
}
_, err := d.Exec("SREM", friendRelationCache+userID, IDList...)
return err
}
func (d *DataBases) GetFriendIDListFromCache(userID string) ([]string, error) {
3 years ago
result, err := redis.Strings(d.Exec("SMEMBERS", friendRelationCache+userID))
return result, err
}
3 years ago
func (d *DataBases) AddBlackUserToCache(userID string, blackList ...string) error {
var IDList []interface{}
for _, id := range blackList {
IDList = append(IDList, id)
}
_, err := d.Exec("SADD", blackListCache+userID, IDList...)
return err
}
3 years ago
func (d *DataBases) ReduceBlackUserFromCache(userID string, blackList ...string) error {
var IDList []interface{}
for _, id := range blackList {
IDList = append(IDList, id)
}
_, err := d.Exec("SREM", blackListCache+userID, IDList...)
return err
}
func (d *DataBases) GetBlackListFromCache(userID string) ([]string, error) {
3 years ago
result, err := redis.Strings(d.Exec("SMEMBERS", blackListCache+userID))
return result, err
}
3 years ago
func (d *DataBases) AddGroupMemberToCache(groupID string, userIDList ...string) error {
var IDList []interface{}
for _, id := range userIDList {
IDList = append(IDList, id)
}
3 years ago
_, err := d.Exec("SADD", groupCache+groupID, IDList...)
3 years ago
return err
}
func (d *DataBases) ReduceGroupMemberFromCache(groupID string, userIDList ...string) error {
var IDList []interface{}
for _, id := range userIDList {
IDList = append(IDList, id)
}
3 years ago
_, err := d.Exec("SREM", groupCache+groupID, IDList...)
3 years ago
return err
}
func (d *DataBases) GetGroupMemberIDListFromCache(groupID string) ([]string, error) {
result, err := redis.Strings(d.Exec("SMEMBERS", groupCache+groupID))
return result, err
}
func (d *DataBases) GetMessageListBySeq(userID string, seqList []uint32, operationID string) (seqMsg []*pbCommon.MsgData, failedSeqList []uint32, errResult error) {
for _, v := range seqList {
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
key := messageCache + userID + "_" + strconv.Itoa(int(v))
result, err := redis.String(d.Exec("GET", key))
if err != nil {
errResult = err
failedSeqList = append(failedSeqList, v)
log2.NewWarn(operationID, "redis get message error:", err.Error(), v)
} else {
msg := pbCommon.MsgData{}
err = jsonpb.UnmarshalString(result, &msg)
if err != nil {
errResult = err
failedSeqList = append(failedSeqList, v)
log2.NewWarn(operationID, "Unmarshal err", result, err.Error())
} else {
log2.NewDebug(operationID, "redis get msg is ", msg.String())
seqMsg = append(seqMsg, &msg)
}
}
}
return seqMsg, failedSeqList, errResult
}
3 years ago
func (d *DataBases) SetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string, operationID string) error {
3 years ago
var failedList []pbChat.MsgDataToMQ
for _, msg := range msgList {
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
s, err := utils.Pb2String(msg.MsgData)
3 years ago
if err != nil {
log2.NewWarn(operationID, utils.GetSelfFuncName(), "Pb2String failed", msg.MsgData.String(), uid, err.Error())
3 years ago
continue
}
log2.NewDebug(operationID, "convert string is ", s)
_, err = d.Exec("SET", key, s, "ex", config.Config.MsgCacheTimeout)
3 years ago
if err != nil {
log2.NewWarn(operationID, utils.GetSelfFuncName(), "redis failed", "args:", key, *msg, uid, s)
3 years ago
failedList = append(failedList, *msg)
}
}
if len(failedList) != 0 {
return errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %s", failedList))
}
return nil
3 years ago
}