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

97 lines
2.8 KiB
Go
Raw Normal View History

2023-07-04 11:15:20 +08:00
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2023-06-30 09:45:02 +08:00
package cache
import (
"context"
"time"
"github.com/dtm-labs/rockscache"
2024-03-10 10:24:20 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/cachekey"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/log"
2024-03-05 10:51:55 +08:00
"github.com/redis/go-redis/v9"
2023-06-30 09:45:02 +08:00
)
const (
blackIDsKey = "BLACK_IDS:"
blackExpireTime = time.Second * 60 * 60 * 12
)
// args fn will exec when no data in msgCache.
2023-06-30 09:45:02 +08:00
type BlackCache interface {
// get blackIDs from msgCache
2023-06-30 09:45:02 +08:00
metaCache
NewCache() BlackCache
GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error)
// del user's blackIDs msgCache, exec when a user's black list changed
2023-06-30 09:45:02 +08:00
DelBlackIDs(ctx context.Context, userID string) BlackCache
}
type BlackCacheRedis struct {
metaCache
expireTime time.Duration
rcClient *rockscache.Client
blackDB relationtb.BlackModelInterface
2023-06-30 09:45:02 +08:00
}
2024-04-19 22:23:08 +08:00
func NewBlackCacheRedis(rdb redis.UniversalClient, localCache *config.LocalCache, blackDB relationtb.BlackModelInterface, options rockscache.Options) BlackCache {
2023-06-30 09:45:02 +08:00
rcClient := rockscache.NewClient(rdb, options)
mc := NewMetaCacheRedis(rcClient)
2024-04-19 22:23:08 +08:00
b := localCache.Friend
log.ZDebug(context.Background(), "black local cache init", "Topic", b.Topic, "SlotNum", b.SlotNum, "SlotSize", b.SlotSize, "enable", b.Enable())
mc.SetTopic(b.Topic)
mc.SetRawRedisClient(rdb)
2023-06-30 09:45:02 +08:00
return &BlackCacheRedis{
expireTime: blackExpireTime,
rcClient: rcClient,
metaCache: mc,
2023-06-30 09:45:02 +08:00
blackDB: blackDB,
}
}
func (b *BlackCacheRedis) NewCache() BlackCache {
2023-10-24 20:28:22 +08:00
return &BlackCacheRedis{
expireTime: b.expireTime,
rcClient: b.rcClient,
blackDB: b.blackDB,
metaCache: b.Copy(),
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(ctx context.Context, userID string) BlackCache {
cache := b.NewCache()
cache.AddKeys(b.getBlackIDsKey(userID))
2023-10-23 16:24:55 +08:00
2023-06-30 09:45:02 +08:00
return cache
}