mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 08:05:58 +08:00
add conversationCache
This commit is contained in:
@@ -6,16 +6,18 @@ import (
|
||||
"Open_IM/pkg/utils"
|
||||
)
|
||||
|
||||
func SetConversation(conversation db.Conversation) error {
|
||||
func SetConversation(conversation db.Conversation) (bool, error) {
|
||||
var isUpdate bool
|
||||
newConversation := conversation
|
||||
if db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Find(&newConversation).RowsAffected == 0 {
|
||||
log.NewDebug("", utils.GetSelfFuncName(), "conversation", conversation, "not exist in db, create")
|
||||
return db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Create(conversation).Error
|
||||
return isUpdate, db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Create(conversation).Error
|
||||
// if exist, then update record
|
||||
} else {
|
||||
log.NewDebug("", utils.GetSelfFuncName(), "conversation", conversation, "exist in db, update")
|
||||
//force update
|
||||
return db.DB.MysqlDB.DefaultGormDB().Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
|
||||
isUpdate = true
|
||||
return isUpdate, db.DB.MysqlDB.DefaultGormDB().Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
|
||||
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt, "is_pinned": conversation.IsPinned, "is_private_chat": conversation.IsPrivateChat,
|
||||
"group_at_type": conversation.GroupAtType, "is_not_in_group": conversation.IsNotInGroup}).Error
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ func GetConversationFromCache(ownerUserID, conversationID string) (*db.Conversat
|
||||
bytes, err := json.Marshal(conversation)
|
||||
return string(bytes), utils.Wrap(err, "")
|
||||
}
|
||||
conversationStr, err := db.DB.Rc.Fetch(conversationCache+conversationID, time.Second*30*60, getConversation)
|
||||
conversationStr, err := db.DB.Rc.Fetch(conversationCache+ownerUserID+":"+conversationID, time.Second*30*60, getConversation)
|
||||
conversation := db.Conversation{}
|
||||
err = json.Unmarshal([]byte(conversationStr), &conversation)
|
||||
if err != nil {
|
||||
@@ -474,6 +474,18 @@ func GetConversationFromCache(ownerUserID, conversationID string) (*db.Conversat
|
||||
return &conversation, nil
|
||||
}
|
||||
|
||||
func GetConversationsFromCache(ownerUserID string, conversationIDList []string) ([]db.Conversation, error) {
|
||||
var conversationList []db.Conversation
|
||||
for _, conversationID := range conversationIDList {
|
||||
conversation, err := GetConversationFromCache(ownerUserID, conversationID)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "GetConversationFromCache failed")
|
||||
}
|
||||
conversationList = append(conversationList, *conversation)
|
||||
}
|
||||
return conversationList, nil
|
||||
}
|
||||
|
||||
func GetUserAllConversationList(ownerUserID string) ([]db.Conversation, error) {
|
||||
IDList, err := GetUserConversationIDListFromCache(ownerUserID)
|
||||
if err != nil {
|
||||
@@ -490,6 +502,6 @@ func GetUserAllConversationList(ownerUserID string) ([]db.Conversation, error) {
|
||||
return conversationList, nil
|
||||
}
|
||||
|
||||
func DelConversationFromCache(conversationID string) error {
|
||||
return db.DB.Rc.TagAsDeleted(conversationCache + conversationID)
|
||||
func DelConversationFromCache(ownerUserID, conversationID string) error {
|
||||
return db.DB.Rc.TagAsDeleted(conversationCache + ownerUserID + ":" + conversationID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user