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

66 lines
1.6 KiB
Go
Raw Normal View History

package redis
2023-06-30 09:45:02 +08:00
import (
"context"
"time"
2024-03-10 10:24:20 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
2024-03-05 10:51:55 +08:00
"github.com/redis/go-redis/v9"
2023-06-30 09:45:02 +08:00
)
const (
blackExpireTime = time.Second * 60 * 60 * 12
)
type BlackCacheRedis struct {
cache.BatchDeleter
2023-06-30 09:45:02 +08:00
expireTime time.Duration
rcClient *rocksCacheClient
blackDB database.Black
2023-06-30 09:45:02 +08:00
}
func NewBlackCacheRedis(rdb redis.UniversalClient, localCache *config.LocalCache, blackDB database.Black) cache.BlackCache {
rc := newRocksCacheClient(rdb)
2023-06-30 09:45:02 +08:00
return &BlackCacheRedis{
BatchDeleter: rc.GetBatchDeleter(localCache.Friend.Topic),
expireTime: blackExpireTime,
rcClient: rc,
blackDB: blackDB,
2023-06-30 09:45:02 +08:00
}
}
func (b *BlackCacheRedis) CloneBlackCache() cache.BlackCache {
2023-10-24 20:28:22 +08:00
return &BlackCacheRedis{
BatchDeleter: b.BatchDeleter.Clone(),
expireTime: b.expireTime,
rcClient: b.rcClient,
blackDB: b.blackDB,
2023-10-24 20:28:22 +08:00
}
2023-06-30 09:45:02 +08:00
}
func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
return cachekey.GetBlackIDsKey(ownerUserID)
2023-06-30 09:45:02 +08:00
}
func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
2023-10-24 20:28:22 +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-06-30 09:45:02 +08:00
}
func (b *BlackCacheRedis) DelBlackIDs(_ context.Context, userID string) cache.BlackCache {
cache := b.CloneBlackCache()
2023-06-30 09:45:02 +08:00
cache.AddKeys(b.getBlackIDsKey(userID))
2023-10-23 16:24:55 +08:00
2023-06-30 09:45:02 +08:00
return cache
}