conversation.go

This commit is contained in:
withchao 2023-02-08 19:18:11 +08:00
parent ec43f2847e
commit b3ae69ce94

View File

@ -2,6 +2,7 @@ package cache
import ( import (
"Open_IM/pkg/common/db/relation" "Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog" "Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"context" "context"
@ -71,7 +72,7 @@ func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUse
// return nil, utils.Wrap(err, "") // return nil, utils.Wrap(err, "")
//} //}
//return conversationIDs, nil //return conversationIDs, nil
return GetCache(c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, func() ([]string, error) { return GetCache(ctx, c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, func(ctx context.Context) ([]string, error) {
return f(ownerUserID) return f(ownerUserID)
}) })
} }
@ -100,32 +101,32 @@ func (c *ConversationCache) GetUserConversationIDs1(ctx context.Context, ownerUs
return GetCache1[[]string](c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, fn) return GetCache1[[]string](c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, fn)
} }
func GetCache1[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (any, error)) (T, error) { //func GetCache1[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (any, error)) (T, error) {
v, err := rcClient.Fetch(key, expire, func() (string, error) { // v, err := rcClient.Fetch(key, expire, func() (string, error) {
v, err := fn() // v, err := fn()
if err != nil { // if err != nil {
return "", err // return "", err
} // }
bs, err := json.Marshal(v) // bs, err := json.Marshal(v)
if err != nil { // if err != nil {
return "", utils.Wrap(err, "") // return "", utils.Wrap(err, "")
} // }
return string(bs), nil // return string(bs), nil
}) // })
var t T // var t T
if err != nil { // if err != nil {
return t, err // return t, err
} // }
err = json.Unmarshal([]byte(v), &t) // err = json.Unmarshal([]byte(v), &t)
if err != nil { // if err != nil {
return t, utils.Wrap(err, "") // return t, utils.Wrap(err, "")
} // }
return t, nil // return t, nil
} //}
func GetCache[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (T, error)) (T, error) { func GetCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) {
v, err := rcClient.Fetch(key, expire, func() (string, error) { v, err := rcClient.Fetch(key, expire, func() (string, error) {
v, err := fn() v, err := fn(ctx)
if err != nil { if err != nil {
return "", err return "", err
} }