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.
162 lines
5.4 KiB
162 lines
5.4 KiB
12 months ago
|
// 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.
|
||
|
|
||
6 months ago
|
package redis
|
||
1 year ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/dtm-labs/rockscache"
|
||
6 months ago
|
"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"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
8 months ago
|
"github.com/openimsdk/tools/s3"
|
||
6 months ago
|
"github.com/openimsdk/tools/s3/cont"
|
||
|
"github.com/openimsdk/tools/s3/minio"
|
||
9 months ago
|
"github.com/redis/go-redis/v9"
|
||
6 months ago
|
"time"
|
||
1 year ago
|
)
|
||
|
|
||
6 months ago
|
func NewObjectCacheRedis(rdb redis.UniversalClient, objDB database.ObjectInfo) cache.ObjectCache {
|
||
|
opts := rockscache.NewDefaultOptions()
|
||
|
batchHandler := NewBatchDeleterRedis(rdb, &opts, nil)
|
||
1 year ago
|
return &objectCacheRedis{
|
||
6 months ago
|
BatchDeleter: batchHandler,
|
||
|
rcClient: rockscache.NewClient(rdb, opts),
|
||
|
expireTime: time.Hour * 12,
|
||
|
objDB: objDB,
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
type objectCacheRedis struct {
|
||
6 months ago
|
cache.BatchDeleter
|
||
|
objDB database.ObjectInfo
|
||
1 year ago
|
rcClient *rockscache.Client
|
||
|
expireTime time.Duration
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *objectCacheRedis) getObjectKey(engine string, name string) string {
|
||
|
return cachekey.GetObjectKey(engine, name)
|
||
|
}
|
||
|
|
||
|
func (g *objectCacheRedis) CloneObjectCache() cache.ObjectCache {
|
||
1 year ago
|
return &objectCacheRedis{
|
||
6 months ago
|
BatchDeleter: g.BatchDeleter.Clone(),
|
||
|
rcClient: g.rcClient,
|
||
|
expireTime: g.expireTime,
|
||
|
objDB: g.objDB,
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *objectCacheRedis) DelObjectName(engine string, names ...string) cache.ObjectCache {
|
||
|
objectCache := g.CloneObjectCache()
|
||
1 year ago
|
keys := make([]string, 0, len(names))
|
||
|
for _, name := range names {
|
||
1 year ago
|
keys = append(keys, g.getObjectKey(name, engine))
|
||
1 year ago
|
}
|
||
|
objectCache.AddKeys(keys...)
|
||
|
return objectCache
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *objectCacheRedis) GetName(ctx context.Context, engine string, name string) (*model.Object, error) {
|
||
|
return getCache(ctx, g.rcClient, g.getObjectKey(name, engine), g.expireTime, func(ctx context.Context) (*model.Object, error) {
|
||
1 year ago
|
return g.objDB.Take(ctx, engine, name)
|
||
1 year ago
|
})
|
||
|
}
|
||
|
|
||
8 months ago
|
func NewS3Cache(rdb redis.UniversalClient, s3 s3.Interface) cont.S3Cache {
|
||
6 months ago
|
opts := rockscache.NewDefaultOptions()
|
||
|
batchHandler := NewBatchDeleterRedis(rdb, &opts, nil)
|
||
1 year ago
|
return &s3CacheRedis{
|
||
6 months ago
|
BatchDeleter: batchHandler,
|
||
|
rcClient: rockscache.NewClient(rdb, opts),
|
||
|
expireTime: time.Hour * 12,
|
||
|
s3: s3,
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
type s3CacheRedis struct {
|
||
6 months ago
|
cache.BatchDeleter
|
||
1 year ago
|
s3 s3.Interface
|
||
|
rcClient *rockscache.Client
|
||
|
expireTime time.Duration
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *s3CacheRedis) getS3Key(engine string, name string) string {
|
||
|
return cachekey.GetS3Key(engine, name)
|
||
1 year ago
|
}
|
||
|
|
||
8 months ago
|
func (g *s3CacheRedis) DelS3Key(ctx context.Context, engine string, keys ...string) error {
|
||
1 year ago
|
ks := make([]string, 0, len(keys))
|
||
|
for _, key := range keys {
|
||
|
ks = append(ks, g.getS3Key(engine, key))
|
||
|
}
|
||
6 months ago
|
return g.BatchDeleter.ExecDelWithKeys(ctx, ks)
|
||
1 year ago
|
}
|
||
|
|
||
|
func (g *s3CacheRedis) GetKey(ctx context.Context, engine string, name string) (*s3.ObjectInfo, error) {
|
||
|
return getCache(ctx, g.rcClient, g.getS3Key(engine, name), g.expireTime, func(ctx context.Context) (*s3.ObjectInfo, error) {
|
||
|
return g.s3.StatObject(ctx, name)
|
||
|
})
|
||
|
}
|
||
|
|
||
8 months ago
|
func NewMinioCache(rdb redis.UniversalClient) minio.Cache {
|
||
6 months ago
|
opts := rockscache.NewDefaultOptions()
|
||
|
batchHandler := NewBatchDeleterRedis(rdb, &opts, nil)
|
||
1 year ago
|
return &minioCacheRedis{
|
||
6 months ago
|
BatchDeleter: batchHandler,
|
||
|
rcClient: rockscache.NewClient(rdb, opts),
|
||
|
expireTime: time.Hour * 24 * 7,
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
type minioCacheRedis struct {
|
||
6 months ago
|
cache.BatchDeleter
|
||
1 year ago
|
rcClient *rockscache.Client
|
||
|
expireTime time.Duration
|
||
|
}
|
||
|
|
||
6 months ago
|
func (g *minioCacheRedis) getObjectImageInfoKey(key string) string {
|
||
|
return cachekey.GetObjectImageInfoKey(key)
|
||
|
}
|
||
|
|
||
|
func (g *minioCacheRedis) getMinioImageThumbnailKey(key string, format string, width int, height int) string {
|
||
|
return cachekey.GetMinioImageThumbnailKey(key, format, width, height)
|
||
1 year ago
|
}
|
||
|
|
||
8 months ago
|
func (g *minioCacheRedis) DelObjectImageInfoKey(ctx context.Context, keys ...string) error {
|
||
1 year ago
|
ks := make([]string, 0, len(keys))
|
||
|
for _, key := range keys {
|
||
|
ks = append(ks, g.getObjectImageInfoKey(key))
|
||
|
}
|
||
6 months ago
|
return g.BatchDeleter.ExecDelWithKeys(ctx, ks)
|
||
1 year ago
|
}
|
||
|
|
||
8 months ago
|
func (g *minioCacheRedis) DelImageThumbnailKey(ctx context.Context, key string, format string, width int, height int) error {
|
||
6 months ago
|
return g.BatchDeleter.ExecDelWithKeys(ctx, []string{g.getMinioImageThumbnailKey(key, format, width, height)})
|
||
1 year ago
|
|
||
|
}
|
||
|
|
||
8 months ago
|
func (g *minioCacheRedis) GetImageObjectKeyInfo(ctx context.Context, key string, fn func(ctx context.Context) (*minio.ImageInfo, error)) (*minio.ImageInfo, error) {
|
||
1 year ago
|
info, err := getCache(ctx, g.rcClient, g.getObjectImageInfoKey(key), g.expireTime, fn)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return info, nil
|
||
|
}
|
||
|
|
||
|
func (g *minioCacheRedis) GetThumbnailKey(ctx context.Context, key string, format string, width int, height int, minioCache func(ctx context.Context) (string, error)) (string, error) {
|
||
|
return getCache(ctx, g.rcClient, g.getMinioImageThumbnailKey(key, format, width, height), g.expireTime, minioCache)
|
||
|
}
|