Files
open-im-server/pkg/common/db/cache/black.go
T

68 lines
1.8 KiB
Go
Raw Normal View History

2023-02-01 11:23:01 +08:00
package cache
import (
"context"
2023-03-23 19:02:20 +08:00
"time"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
2023-02-01 11:23:01 +08:00
"github.com/dtm-labs/rockscache"
2023-02-02 19:40:54 +08:00
"github.com/go-redis/redis/v8"
2023-02-01 11:23:01 +08:00
)
const (
blackIDsKey = "BLACK_IDS:"
blackExpireTime = time.Second * 60 * 60 * 12
)
2023-02-09 14:40:49 +08:00
// args fn will exec when no data in cache
2023-02-09 10:55:21 +08:00
type BlackCache interface {
//get blackIDs from cache
2023-03-23 19:02:20 +08:00
metaCache
NewCache() BlackCache
GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error)
2023-02-09 10:55:21 +08:00
//del user's blackIDs cache, exec when a user's black list changed
2023-03-23 19:02:20 +08:00
DelBlackIDs(ctx context.Context, userID string) BlackCache
2023-02-09 10:55:21 +08:00
}
type BlackCacheRedis struct {
2023-03-23 19:02:20 +08:00
metaCache
2023-02-01 11:23:01 +08:00
expireTime time.Duration
rcClient *rockscache.Client
2023-03-23 19:02:20 +08:00
blackDB relationTb.BlackModelInterface
2023-02-01 11:23:01 +08:00
}
2023-03-23 19:02:20 +08:00
func NewBlackCacheRedis(rdb redis.UniversalClient, blackDB relationTb.BlackModelInterface, options rockscache.Options) BlackCache {
rcClient := rockscache.NewClient(rdb, options)
2023-02-09 10:55:21 +08:00
return &BlackCacheRedis{
2023-02-02 19:40:54 +08:00
expireTime: blackExpireTime,
2023-03-23 19:02:20 +08:00
rcClient: rcClient,
metaCache: NewMetaCacheRedis(rcClient),
blackDB: blackDB,
}
}
func (b *BlackCacheRedis) NewCache() BlackCache {
return &BlackCacheRedis{
expireTime: b.expireTime,
rcClient: b.rcClient,
blackDB: b.blackDB,
2023-03-27 17:20:36 +08:00
metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...),
2023-02-01 11:23:01 +08:00
}
}
2023-02-09 10:55:21 +08:00
func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
2023-02-01 11:23:01 +08:00
return blackIDsKey + ownerUserID
}
2023-02-09 10:55:21 +08:00
func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
2023-03-23 19:02:20 +08:00
return getCache(ctx, b.rcClient, b.getBlackIDsKey(userID), b.expireTime, func(ctx context.Context) ([]string, error) {
return b.blackDB.FindBlackUserIDs(ctx, userID)
2023-02-20 10:49:39 +08:00
})
2023-02-01 11:23:01 +08:00
}
2023-03-23 19:02:20 +08:00
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache {
cache := b.NewCache()
cache.AddKeys(userID)
return cache
2023-02-01 11:23:01 +08:00
}