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

55 lines
1.6 KiB

2 years ago
package cache
import (
2 years ago
"OpenIM/pkg/common/db/relation"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/utils"
2 years ago
"context"
"github.com/dtm-labs/rockscache"
2 years ago
"github.com/go-redis/redis/v8"
2 years ago
"time"
)
const (
blackIDsKey = "BLACK_IDS:"
blackExpireTime = time.Second * 60 * 60 * 12
)
2 years ago
// args fn will exec when no data in cache
2 years ago
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 {
2 years ago
expireTime time.Duration
rcClient *rockscache.Client
2 years ago
black *relation.BlackGorm
2 years ago
}
2 years ago
func NewBlackCacheRedis(rdb redis.UniversalClient, blackDB BlackCache, options rockscache.Options) *BlackCacheRedis {
return &BlackCacheRedis{
2 years ago
expireTime: blackExpireTime,
rcClient: rockscache.NewClient(rdb, options),
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.black.FindBlackUserIDs(ctx, userID)
})
2 years ago
}
2 years ago
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) (err error) {
2 years ago
defer func() {
2 years ago
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
2 years ago
}()
2 years ago
return b.rcClient.TagAsDeleted(b.getBlackIDsKey(userID))
2 years ago
}