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

83 lines
2.6 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.
package redis
2023-06-30 09:45:02 +08:00
import (
"context"
"github.com/dtm-labs/rockscache"
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-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"
"time"
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 *rockscache.Client
blackDB database.Black
2023-06-30 09:45:02 +08:00
}
func NewBlackCacheRedis(rdb redis.UniversalClient, localCache *config.LocalCache, blackDB database.Black, options *rockscache.Options) cache.BlackCache {
batchHandler := NewBatchDeleterRedis(rdb, options, []string{localCache.Friend.Topic})
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())
2023-06-30 09:45:02 +08:00
return &BlackCacheRedis{
BatchDeleter: batchHandler,
expireTime: blackExpireTime,
rcClient: rockscache.NewClient(rdb, *options),
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
}