v2
This commit is contained in:
@@ -13,31 +13,42 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
engine *redis.Client
|
||||
ErrRedisKeyNotFound = errors.New("redis key not found")
|
||||
)
|
||||
var ErrRedisKeyNotFound = errors.New("redis key not found")
|
||||
|
||||
// func GetRedis() *redis.Client {
|
||||
// return rd
|
||||
// }
|
||||
type IRedis 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
|
||||
Get(ctx context.Context, key string) (string, error)
|
||||
GetBytes(ctx context.Context, key string) ([]byte, error)
|
||||
Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd
|
||||
Keys(ctx context.Context, pattern string) ([]string, error)
|
||||
ListKeys(ctx context.Context, pattern string, pageID int, pageSize int) ([]string, int, error)
|
||||
}
|
||||
|
||||
func Init() error {
|
||||
type redisCache struct {
|
||||
engine *redis.Client
|
||||
}
|
||||
|
||||
var _ IRedis = (*redisCache)(nil)
|
||||
|
||||
func New(conf config.Redis) (*redisCache, error) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", config.File.Redis.Host, config.File.Redis.Port),
|
||||
Password: config.File.Redis.Password,
|
||||
DB: config.File.Redis.DB,
|
||||
Addr: fmt.Sprintf("%s:%d", conf.Host, conf.Port),
|
||||
Password: conf.Password,
|
||||
DB: conf.DB,
|
||||
})
|
||||
_, err := rdb.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
engine = rdb
|
||||
return nil
|
||||
return &redisCache{
|
||||
engine: rdb,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Encode(a any) ([]byte, error) {
|
||||
func (r *redisCache) Encode(a any) ([]byte, error) {
|
||||
var b bytes.Buffer
|
||||
if err := gob.NewEncoder(&b).Encode(a); err != nil {
|
||||
return nil, err
|
||||
@@ -47,18 +58,18 @@ func Encode(a any) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Set 设置值
|
||||
func Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
return engine.Set(ctx, key, value, expiration).Err()
|
||||
func (r *redisCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||||
return r.engine.Set(ctx, key, value, expiration).Err()
|
||||
}
|
||||
|
||||
// Del 删除键值
|
||||
func Del(ctx context.Context, keys ...string) error {
|
||||
return engine.Del(ctx, keys...).Err()
|
||||
func (r *redisCache) Del(ctx context.Context, keys ...string) error {
|
||||
return r.engine.Del(ctx, keys...).Err()
|
||||
}
|
||||
|
||||
// Get 获取值
|
||||
func Get(ctx context.Context, key string) (string, error) {
|
||||
val, err := engine.Get(ctx, key).Result()
|
||||
func (r *redisCache) Get(ctx context.Context, key string) (string, error) {
|
||||
val, err := r.engine.Get(ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
return "", ErrRedisKeyNotFound
|
||||
} else if err != nil {
|
||||
@@ -69,8 +80,8 @@ func Get(ctx context.Context, key string) (string, error) {
|
||||
}
|
||||
|
||||
// GetBytes 获取值
|
||||
func GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
val, err := engine.Get(ctx, key).Bytes()
|
||||
func (r *redisCache) GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
val, err := r.engine.Get(ctx, key).Bytes()
|
||||
if err == redis.Nil {
|
||||
return nil, ErrRedisKeyNotFound
|
||||
} else if err != nil {
|
||||
@@ -80,16 +91,16 @@ func GetBytes(ctx context.Context, key string) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd {
|
||||
return engine.Scan(ctx, cursor, match, count)
|
||||
func (r *redisCache) Scan(ctx context.Context, cursor uint64, match string, count int64) *redis.ScanCmd {
|
||||
return r.engine.Scan(ctx, cursor, match, count)
|
||||
}
|
||||
|
||||
func Keys(ctx context.Context, pattern string) ([]string, error) {
|
||||
return engine.Keys(ctx, pattern).Result()
|
||||
func (r *redisCache) Keys(ctx context.Context, pattern string) ([]string, error) {
|
||||
return r.engine.Keys(ctx, pattern).Result()
|
||||
}
|
||||
|
||||
func ListKeys(ctx context.Context, pattern string, pageID int, pageSize int) ([]string, int, error) {
|
||||
all, err := engine.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()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -104,7 +115,7 @@ func ListKeys(ctx context.Context, pattern string, pageID int, pageSize int) ([]
|
||||
for {
|
||||
var scanResult []string
|
||||
var err error
|
||||
scanResult, cursor, err = engine.Scan(ctx, cursor, pattern, int64(pageSize)).Result()
|
||||
scanResult, cursor, err = r.engine.Scan(ctx, cursor, pattern, int64(pageSize)).Result()
|
||||
if err != nil {
|
||||
return nil, count, err
|
||||
}
|
||||
@@ -124,3 +135,36 @@ func ListKeys(ctx context.Context, pattern string, pageID int, pageSize int) ([]
|
||||
}
|
||||
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