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/black.go

68 lines
1.8 KiB

2 years ago
package cache
import (
"context"
2 years ago
"time"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
2 years ago
"github.com/dtm-labs/rockscache"
2 years ago
"github.com/go-redis/redis/v8"
2 years ago
)
const (
blackIDsKey = "BLACK_IDS:"
blackExpireTime = time.Second * 60 * 60 * 12
)
2 years ago
// args fn will exec when no data in msgCache
2 years ago
type BlackCache interface {
2 years ago
//get blackIDs from msgCache
2 years ago
metaCache
NewCache() BlackCache
GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error)
2 years ago
//del user's blackIDs msgCache, exec when a user's black list changed
2 years ago
DelBlackIDs(ctx context.Context, userID string) BlackCache
2 years ago
}
type BlackCacheRedis struct {
2 years ago
metaCache
2 years ago
expireTime time.Duration
rcClient *rockscache.Client
2 years ago
blackDB relationTb.BlackModelInterface
2 years ago
}
2 years ago
func NewBlackCacheRedis(rdb redis.UniversalClient, blackDB relationTb.BlackModelInterface, options rockscache.Options) BlackCache {
rcClient := rockscache.NewClient(rdb, options)
2 years ago
return &BlackCacheRedis{
2 years ago
expireTime: blackExpireTime,
2 years ago
rcClient: rcClient,
metaCache: NewMetaCacheRedis(rcClient),
blackDB: blackDB,
}
}
func (b *BlackCacheRedis) NewCache() BlackCache {
return &BlackCacheRedis{
expireTime: b.expireTime,
rcClient: b.rcClient,
blackDB: b.blackDB,
2 years ago
metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...),
2 years ago
}
}
2 years ago
func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
2 years ago
return blackIDsKey + ownerUserID
}
2 years ago
func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
2 years ago
return getCache(ctx, b.rcClient, b.getBlackIDsKey(userID), b.expireTime, func(ctx context.Context) ([]string, error) {
return b.blackDB.FindBlackUserIDs(ctx, userID)
2 years ago
})
2 years ago
}
2 years ago
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache {
cache := b.NewCache()
cache.AddKeys(userID)
return cache
2 years ago
}