gorm wire
This commit is contained in:
@@ -10,12 +10,13 @@ import (
|
||||
|
||||
"management/internal/pkg/config"
|
||||
|
||||
"github.com/drhin/logger"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ErrRedisKeyNotFound = errors.New("redis key not found")
|
||||
|
||||
type RedisCache interface {
|
||||
type Cache interface {
|
||||
Encode(a any) ([]byte, error)
|
||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
|
||||
Del(ctx context.Context, keys ...string) error
|
||||
@@ -27,25 +28,29 @@ type RedisCache interface {
|
||||
}
|
||||
|
||||
type redisCache struct {
|
||||
engine *redis.Client
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
var _ RedisCache = (*redisCache)(nil)
|
||||
|
||||
func New(conf config.Redis) (*redisCache, error) {
|
||||
func New(conf *config.Config, log *logger.Logger) (Cache, func(), error) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", conf.Host, conf.Port),
|
||||
Password: conf.Password,
|
||||
DB: conf.DB,
|
||||
Addr: fmt.Sprintf("%s:%d", conf.Redis.Host, conf.Redis.Port),
|
||||
Password: conf.Redis.Password,
|
||||
DB: conf.Redis.DB,
|
||||
})
|
||||
_, err := rdb.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cleanup := func() {
|
||||
if err := rdb.Close(); err != nil {
|
||||
log.Error("redis close error", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &redisCache{
|
||||
engine: rdb,
|
||||
}, nil
|
||||
client: rdb,
|
||||
}, cleanup, nil
|
||||
}
|
||||
|
||||
func (r *redisCache) Encode(a any) ([]byte, error) {
|
||||
@@ -59,18 +64,18 @@ func (r *redisCache) Encode(a any) ([]byte, error) {
|
||||
|
||||
// Set 设置值
|
||||
func (r *redisCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
return r.engine.Set(ctx, key, value, expiration).Err()
|
||||
return r.client.Set(ctx, key, value, expiration).Err()
|
||||
}
|
||||
|
||||
// Del 删除键值
|
||||
func (r *redisCache) Del(ctx context.Context, keys ...string) error {
|
||||
return r.engine.Del(ctx, keys...).Err()
|
||||
return r.client.Del(ctx, keys...).Err()
|
||||
}
|
||||
|
||||
// Get 获取值
|
||||
func (r *redisCache) Get(ctx context.Context, key string) (string, error) {
|
||||
val, err := r.engine.Get(ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
val, err := r.client.Get(ctx, key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return "", ErrRedisKeyNotFound
|
||||
} else if err != nil {
|
||||
return "", fmt.Errorf("cannot get value with:[%s]: %v", key, err)
|
||||
@@ -81,8 +86,8 @@ func (r *redisCache) Get(ctx context.Context, key string) (string, error) {
|
||||
|
||||
// GetBytes 获取值
|
||||
func (r *redisCache) GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
val, err := r.engine.Get(ctx, key).Bytes()
|
||||
if err == redis.Nil {
|
||||
val, err := r.client.Get(ctx, key).Bytes()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, ErrRedisKeyNotFound
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("cannot get value with:[%s]: %v", key, err)
|
||||
@@ -92,15 +97,15 @@ func (r *redisCache) GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (r *redisCache) Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd {
|
||||
return r.engine.Scan(ctx, cursor, match, count)
|
||||
return r.client.Scan(ctx, cursor, match, count)
|
||||
}
|
||||
|
||||
func (r *redisCache) Keys(ctx context.Context, pattern string) ([]string, error) {
|
||||
return r.engine.Keys(ctx, pattern).Result()
|
||||
return r.client.Keys(ctx, pattern).Result()
|
||||
}
|
||||
|
||||
func (r *redisCache) ListKeys(ctx context.Context, pattern string, pageID int, pageSize int) ([]string, int, error) {
|
||||
all, err := r.engine.Keys(ctx, pattern).Result()
|
||||
all, err := r.client.Keys(ctx, pattern).Result()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -115,7 +120,7 @@ func (r *redisCache) ListKeys(ctx context.Context, pattern string, pageID int, p
|
||||
for {
|
||||
var scanResult []string
|
||||
var err error
|
||||
scanResult, cursor, err = r.engine.Scan(ctx, cursor, pattern, int64(pageSize)).Result()
|
||||
scanResult, cursor, err = r.client.Scan(ctx, cursor, pattern, int64(pageSize)).Result()
|
||||
if err != nil {
|
||||
return nil, count, err
|
||||
}
|
||||
@@ -135,36 +140,3 @@ func (r *redisCache) ListKeys(ctx context.Context, pattern string, pageID int, p
|
||||
}
|
||||
return keys[startIndex:endIndex], count, nil
|
||||
}
|
||||
|
||||
// ==========================
|
||||
func Encode(a any) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Del(ctx context.Context, keys ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Get(ctx context.Context, key string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Keys(ctx context.Context, pattern string) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func ListKeys(ctx context.Context, pattern string, pageID int, pageSize int) ([]string, int, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user