From f98d894a4bcbdda841b40f47c94b86e5a3efa117 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 3 Feb 2023 14:36:38 +0800 Subject: [PATCH 1/3] 1 --- pkg/utils/utils_v2.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/utils/utils_v2.go b/pkg/utils/utils_v2.go index 029e70496..c2b5d7c10 100644 --- a/pkg/utils/utils_v2.go +++ b/pkg/utils/utils_v2.go @@ -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) From 64dee9f989a90cb21b498088de9a2f4671644aeb Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 3 Feb 2023 16:10:07 +0800 Subject: [PATCH 2/3] 1 --- pkg/utils/utils_v2.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/utils/utils_v2.go b/pkg/utils/utils_v2.go index c2b5d7c10..cabf9067e 100644 --- a/pkg/utils/utils_v2.go +++ b/pkg/utils/utils_v2.go @@ -244,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)) @@ -290,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) From a2c4bccdb6501fee305e9304b9cfadf1c8384a52 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 3 Feb 2023 16:10:51 +0800 Subject: [PATCH 3/3] 1 --- pkg/common/db/cache/conversation.go | 95 +++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/pkg/common/db/cache/conversation.go b/pkg/common/db/cache/conversation.go index 7df8b9725..0a4dd3711 100644 --- a/pkg/common/db/cache/conversation.go +++ b/pkg/common/db/cache/conversation.go @@ -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) {