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

96 lines
2.7 KiB
Go
Raw Normal View History

package redis
2023-11-02 20:40:45 -05:00
import (
"context"
"time"
"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"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/s3"
"github.com/openimsdk/tools/s3/cont"
2024-03-05 10:51:55 +08:00
"github.com/redis/go-redis/v9"
2023-11-02 20:40:45 -05:00
)
func NewObjectCacheRedis(rdb redis.UniversalClient, objDB database.ObjectInfo) cache.ObjectCache {
rc := newRocksCacheClient(rdb)
2023-11-02 20:40:45 -05:00
return &objectCacheRedis{
BatchDeleter: rc.GetBatchDeleter(),
rcClient: rc,
expireTime: time.Hour * 12,
objDB: objDB,
2023-11-02 20:40:45 -05:00
}
}
type objectCacheRedis struct {
cache.BatchDeleter
objDB database.ObjectInfo
rcClient *rocksCacheClient
2023-11-02 20:40:45 -05:00
expireTime time.Duration
}
func (g *objectCacheRedis) getObjectKey(engine string, name string) string {
return cachekey.GetObjectKey(engine, name)
}
func (g *objectCacheRedis) CloneObjectCache() cache.ObjectCache {
2023-11-02 20:40:45 -05:00
return &objectCacheRedis{
BatchDeleter: g.BatchDeleter.Clone(),
rcClient: g.rcClient,
expireTime: g.expireTime,
objDB: g.objDB,
2023-11-02 20:40:45 -05:00
}
}
func (g *objectCacheRedis) DelObjectName(engine string, names ...string) cache.ObjectCache {
objectCache := g.CloneObjectCache()
2023-11-02 20:40:45 -05:00
keys := make([]string, 0, len(names))
for _, name := range names {
keys = append(keys, g.getObjectKey(name, engine))
2023-11-02 20:40:45 -05:00
}
objectCache.AddKeys(keys...)
return objectCache
}
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) {
return g.objDB.Take(ctx, engine, name)
2023-11-02 20:40:45 -05:00
})
}
2024-04-19 22:23:08 +08:00
func NewS3Cache(rdb redis.UniversalClient, s3 s3.Interface) cont.S3Cache {
rc := newRocksCacheClient(rdb)
2023-11-02 20:40:45 -05:00
return &s3CacheRedis{
BatchDeleter: rc.GetBatchDeleter(),
rcClient: rc,
expireTime: time.Hour * 12,
s3: s3,
2023-11-02 20:40:45 -05:00
}
}
type s3CacheRedis struct {
cache.BatchDeleter
2023-11-02 20:40:45 -05:00
s3 s3.Interface
rcClient *rocksCacheClient
2023-11-02 20:40:45 -05:00
expireTime time.Duration
}
func (g *s3CacheRedis) getS3Key(engine string, name string) string {
return cachekey.GetS3Key(engine, name)
2023-11-02 20:40:45 -05:00
}
2024-04-19 22:23:08 +08:00
func (g *s3CacheRedis) DelS3Key(ctx context.Context, engine string, keys ...string) error {
2023-11-02 20:40:45 -05:00
ks := make([]string, 0, len(keys))
for _, key := range keys {
ks = append(ks, g.getS3Key(engine, key))
}
return g.BatchDeleter.ExecDelWithKeys(ctx, ks)
2023-11-02 20:40:45 -05:00
}
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)
})
}