conversation cache function

This commit is contained in:
wangchuxiao
2022-08-19 12:09:05 +08:00
parent 9138f90843
commit 761b8c40d6
2 changed files with 70 additions and 0 deletions
+62
View File
@@ -32,6 +32,8 @@ const (
joinedSuperGroupListCache = "JOINED_SUPER_GROUP_LIST_CACHE:"
groupMemberListHashCache = "GROUP_MEMBER_LIST_HASH_CACHE:"
groupMemberNumCache = "GROUP_MEMBER_NUM_CACHE:"
conversationCache = "CONVERSATION_CACHE:"
conversationIDListCache = "CONVERSATION_ID_LIST_CACHE:"
)
func init() {
@@ -431,3 +433,63 @@ func GetGroupMemberNumFromCache(groupID string) (int64, error) {
func DelGroupMemberNumFromCache(groupID string) error {
return db.DB.Rc.TagAsDeleted(groupMemberNumCache + groupID)
}
func GetUserConversationIDListFromCache(userID string) ([]string, error) {
getConversationIDList := func() (string, error) {
conversationIDList, err := imdb.GetConversationIDListByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "getConversationIDList failed")
}
bytes, err := json.Marshal(conversationIDList)
return string(bytes), utils.Wrap(err, "")
}
conversationIDListStr, err := db.DB.Rc.Fetch(conversationIDListCache+userID, time.Second*30*60, getConversationIDList)
var conversationIDList []string
err = json.Unmarshal([]byte(conversationIDListStr), &conversationIDList)
if err != nil {
return nil, err
}
return conversationIDList, nil
}
func DelUserConversationIDListFromCache(userID string) error {
return db.DB.Rc.TagAsDeleted(conversationIDListCache + userID)
}
func GetConversationFromCache(ownerUserID, conversationID string) (*db.Conversation, error) {
getConversation := func() (string, error) {
conversation, err := imdb.GetConversation(ownerUserID, conversationID)
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(conversation)
return string(bytes), utils.Wrap(err, "")
}
conversationStr, err := db.DB.Rc.Fetch(conversationCache+conversationID, time.Second*30*60, getConversation)
conversation := db.Conversation{}
err = json.Unmarshal([]byte(conversationStr), &conversation)
if err != nil {
return nil, err
}
return &conversation, nil
}
func GetUserAllConversationList(ownerUserID string) ([]db.Conversation, error) {
IDList, err := GetUserConversationIDListFromCache(ownerUserID)
if err != nil {
return nil, err
}
var conversationList []db.Conversation
for _, conversationID := range IDList {
conversation, err := GetConversationFromCache(ownerUserID, conversationID)
if err != nil {
return nil, utils.Wrap(err, "GetConversationFromCache failed")
}
conversationList = append(conversationList, *conversation)
}
return conversationList, nil
}
func DelConversationFromCache(conversationID string) error {
return db.DB.Rc.TagAsDeleted(conversationCache + conversationID)
}