You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/cache/rockscache.go

85 lines
1.9 KiB

2 years ago
package cache
2 years ago
import (
"Open_IM/pkg/utils"
"context"
2 years ago
"encoding/json"
2 years ago
"github.com/dtm-labs/rockscache"
2 years ago
"time"
)
2 years ago
const scanCount = 3000
2 years ago
2 years ago
func (rc *RcClient) DelKeys() {
2 years ago
for _, key := range []string{"GROUP_CACHE:", "FRIEND_RELATION_CACHE", "BLACK_LIST_CACHE:", "USER_INFO_CACHE:", "GROUP_INFO_CACHE", groupOwnerIDCache, joinedGroupListCache,
groupMemberInfoCache, groupAllMemberInfoCache, "ALL_FRIEND_INFO_CACHE:"} {
2 years ago
fName := utils.GetSelfFuncName()
var cursor uint64
var n int
for {
var keys []string
var err error
2 years ago
keys, cursor, err = rc.rdb.Scan(context.Background(), cursor, key+"*", scanCount).Result()
if err != nil {
panic(err.Error())
}
n += len(keys)
// for each for redis cluster
2 years ago
for _, key := range keys {
2 years ago
if err = rc.rdb.Del(context.Background(), key).Err(); err != nil {
2 years ago
log.NewError("", fName, key, err.Error())
2 years ago
err = rc.rdb.Del(context.Background(), key).Err()
2 years ago
if err != nil {
panic(err.Error())
}
}
}
if cursor == 0 {
break
}
}
}
}
2 years ago
func GetCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) {
var t T
var write bool
v, err := rcClient.Fetch(key, expire, func() (s string, err error) {
t, err = fn(ctx)
if err != nil {
return "", err
}
bs, err := json.Marshal(t)
if err != nil {
return "", utils.Wrap(err, "")
}
write = true
return string(bs), nil
})
if err != nil {
return t, err
}
if write {
return t, nil
}
err = json.Unmarshal([]byte(v), &t)
if err != nil {
return t, utils.Wrap(err, "")
}
return t, nil
}
func GetCacheFor[E any, T any](ctx context.Context, list []E, fn func(ctx context.Context, item E) (T, error)) ([]T, error) {
rs := make([]T, 0, len(list))
for _, e := range list {
r, err := fn(ctx, e)
if err != nil {
return nil, err
}
rs = append(rs, r)
}
return rs, nil
}