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

50 lines
1.5 KiB
Go
Raw Normal View History

2023-02-01 11:23:01 +08:00
package cache
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/db/relation"
2023-02-01 11:23:01 +08:00
"context"
"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
"time"
)
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
GetBlackIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) ([]string, error)) (blackIDs []string, err error)
//del user's blackIDs cache, exec when a user's black list changed
DelBlackIDs(ctx context.Context, userID string) (err error)
}
type BlackCacheRedis struct {
2023-02-01 11:23:01 +08:00
expireTime time.Duration
rcClient *rockscache.Client
2023-02-20 10:49:39 +08:00
black *relation.BlackGorm
2023-02-01 11:23:01 +08:00
}
2023-02-09 10:55:21 +08:00
func NewBlackCacheRedis(rdb redis.UniversalClient, blackDB BlackCache, options rockscache.Options) *BlackCacheRedis {
return &BlackCacheRedis{
2023-02-02 19:40:54 +08:00
expireTime: blackExpireTime,
rcClient: rockscache.NewClient(rdb, options),
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-02-20 10:49:39 +08:00
return GetCache(ctx, b.rcClient, b.getBlackIDsKey(userID), b.expireTime, func(ctx context.Context) ([]string, error) {
return b.black.FindBlackUserIDs(ctx, userID)
})
2023-02-01 11:23:01 +08:00
}
2023-02-09 10:55:21 +08:00
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) (err error) {
2023-02-20 10:49:39 +08:00
return b.rcClient.TagAsDeleted(b.getBlackIDsKey(userID))
2023-02-01 11:23:01 +08:00
}