Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
skiffer-git 2 years ago
commit 795a90a194

@ -48,26 +48,99 @@ func (c *ConversationCache) getSuperGroupRecvNotNotifyUserIDsKey(groupID string)
}
func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUserID string) (conversationIDs []string, err error) {
getConversationIDs := func() (string, error) {
conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
//getConversationIDs := func() (string, error) {
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
// if err != nil {
// return "", err
// }
// bytes, err := json.Marshal(conversationIDs)
// if err != nil {
// return "", utils.Wrap(err, "")
// }
// return string(bytes), nil
//}
//defer func() {
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationIDs", conversationIDs)
//}()
//conversationIDsStr, err := c.rcClient.Fetch(c.getConversationIDsKey(ownerUserID), time.Second*30*60, getConversationIDs)
//err = json.Unmarshal([]byte(conversationIDsStr), &conversationIDs)
//if err != nil {
// return nil, utils.Wrap(err, "")
//}
//return conversationIDs, nil
return GetCache(c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, func() ([]string, error) {
return relation.GetConversationIDsByUserID(ownerUserID)
})
}
func (c *ConversationCache) GetUserConversationIDs1(ctx context.Context, ownerUserID string, fn func() (any, error)) (conversationIDs []string, err error) {
//getConversationIDs := func() (string, error) {
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
// if err != nil {
// return "", err
// }
// bytes, err := json.Marshal(conversationIDs)
// if err != nil {
// return "", utils.Wrap(err, "")
// }
// return string(bytes), nil
//}
//defer func() {
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationIDs", conversationIDs)
//}()
//conversationIDsStr, err := c.rcClient.Fetch(c.getConversationIDsKey(ownerUserID), time.Second*30*60, getConversationIDs)
//err = json.Unmarshal([]byte(conversationIDsStr), &conversationIDs)
//if err != nil {
// return nil, utils.Wrap(err, "")
//}
//return conversationIDs, nil
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) {
v, err := rcClient.Fetch(key, expire, func() (string, error) {
v, err := fn()
if err != nil {
return "", err
}
bytes, err := json.Marshal(conversationIDs)
bs, err := json.Marshal(v)
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
return string(bs), nil
})
var t T
if err != nil {
return t, err
}
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationIDs", conversationIDs)
}()
conversationIDsStr, err := c.rcClient.Fetch(c.getConversationIDsKey(ownerUserID), time.Second*30*60, getConversationIDs)
err = json.Unmarshal([]byte(conversationIDsStr), &conversationIDs)
err = json.Unmarshal([]byte(v), &t)
if err != nil {
return nil, utils.Wrap(err, "")
return t, utils.Wrap(err, "")
}
return conversationIDs, nil
return t, nil
}
func GetCache[T any](rcClient *rockscache.Client, key string, expire time.Duration, fn func() (T, error)) (T, error) {
v, err := rcClient.Fetch(key, expire, func() (string, error) {
v, err := fn()
if err != nil {
return "", err
}
bs, err := json.Marshal(v)
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bs), nil
})
var t T
if err != nil {
return t, err
}
err = json.Unmarshal([]byte(v), &t)
if err != nil {
return t, utils.Wrap(err, "")
}
return t, nil
}
func (c *ConversationCache) DelUserConversationIDs(ctx context.Context, ownerUserID string) (err error) {

@ -89,6 +89,26 @@ func Contain[E comparable](es []E, e E) bool {
return IndexOf(es, e) >= 0
}
// DuplicateAny judge whether it is repeated
func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool {
t := make(map[K]struct{})
for _, e := range es {
k := fn(e)
if _, ok := t[k]; ok {
return true
}
t[k] = struct{}{}
}
return false
}
// Duplicate judge whether it is repeated
func Duplicate[E comparable](es []E) bool {
return DuplicateAny(es, func(e E) E {
return e
})
}
// SliceToMapOkAny slice to map
func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, bool)) map[K]V {
kv := make(map[K]V)
@ -224,6 +244,22 @@ func CompleteAny[K comparable, E any](ks []K, es []E, fn func(e E) K) bool {
return len(a) == 0
}
func Complete[E comparable](a []E, b []E) bool {
if len(a) == 0 && len(b) == 0 {
return true
}
if (len(a) == 0 && len(b) != 0) || (len(a) != 0 && len(b) == 0) {
return false
}
t := SliceSet(a)
for _, e := range b {
if _, ok := t[e]; !ok {
return false
}
}
return true
}
// MapKey get map keys
func MapKey[K comparable, V any](kv map[K]V) []K {
ks := make([]K, 0, len(kv))
@ -270,6 +306,25 @@ func If[T any](isa bool, a, b T) T {
return b
}
func Equal[E comparable](a []E, b []E) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
// Single
func Single[E comparable](a, b []E) []E {
return nil
}
func UniqueJoin(s ...string) string {
data, _ := json.Marshal(s)
return string(data)

Loading…
Cancel
Save