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.
196 lines
4.9 KiB
196 lines
4.9 KiB
package jredis |
|
|
|
import ( |
|
"context" |
|
"git.gz.internal.jumaiyx.cn/jm/jmproto/conf" |
|
"git.gz.internal.jumaiyx.cn/pkg/log" |
|
"github.com/go-redis/redis/v8" |
|
"github.com/pkg/errors" |
|
"time" |
|
) |
|
|
|
// todo 往后封包 |
|
const ( |
|
NullString = "" |
|
) |
|
|
|
type Cache struct { |
|
log log.Logger |
|
client *redis.Client |
|
} |
|
|
|
func NewCache(log log.Logger, c *conf.Data_Redis) *Cache { |
|
client := redis.NewClient(&redis.Options{ |
|
Network: c.Network, |
|
Addr: c.Addr, |
|
Password: c.Password, |
|
PoolSize: 10, |
|
}) |
|
return &Cache{ |
|
log: log, |
|
client: client, |
|
} |
|
} |
|
|
|
func (rdb Cache) Get(ctx context.Context, key string) (string, error) { |
|
val, err := rdb.client.Get(ctx, key).Result() |
|
if err != nil { |
|
if errors.Is(err, redis.Nil) { |
|
return NullString, nil |
|
} |
|
rdb.log.Errorf("redis cache get err:%v", err) |
|
return NullString, err |
|
} |
|
return val, nil |
|
} |
|
|
|
func (rdb Cache) Set(ctx context.Context, key string, value interface{}) error { |
|
err := rdb.client.Set(ctx, key, value, 0).Err() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache set err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
func (rdb Cache) Incr(ctx context.Context, key string) (int64, error) { |
|
result, err := rdb.client.Incr(ctx, key).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache incr err:%v", err) |
|
return result, err |
|
} |
|
return result, nil |
|
} |
|
|
|
func (rdb Cache) Decr(ctx context.Context, key string) (int64, error) { |
|
result, err := rdb.client.Decr(ctx, key).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache decr err:%v", err) |
|
return result, err |
|
} |
|
return result, nil |
|
} |
|
|
|
func (rdb Cache) SetExp(ctx context.Context, key string, value interface{}, exp time.Duration) error { |
|
err := rdb.client.Set(ctx, key, value, exp).Err() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache set exp err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
func (rdb Cache) Del(ctx context.Context, key string) error { |
|
_, err := rdb.client.Del(ctx, key).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis del err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
func (rdb Cache) ZAdd(ctx context.Context, key string, z *redis.Z) (int64, error) { |
|
result, err := rdb.client.ZAdd(ctx, key, z).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache zadd err:%v", err) |
|
return 0, err |
|
} |
|
return result, err |
|
} |
|
|
|
func (rdb Cache) ZIncrBy(ctx context.Context, key string, member string, incr float64) error { |
|
_, err := rdb.client.ZIncrBy(ctx, key, incr, member).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache zadd err:%v", err) |
|
return err |
|
} |
|
return err |
|
} |
|
|
|
func (rdb Cache) ZRange(ctx context.Context, key string, tart, stop int64) ([]string, error) { |
|
result, err := rdb.client.ZRange(ctx, key, tart, stop).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache zrange err:%v", err) |
|
return nil, err |
|
} |
|
return result, nil |
|
} |
|
|
|
func (rdb Cache) ZRevRange(ctx context.Context, key string, tart, stop int64) ([]string, error) { |
|
result, err := rdb.client.ZRevRange(ctx, key, tart, stop).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache ZRevRange err:%v", err) |
|
return nil, err |
|
} |
|
return result, nil |
|
} |
|
|
|
func (rdb Cache) ZRevRangeWithScores(ctx context.Context, key string, tart, stop int64) ([]redis.Z, error) { |
|
result, err := rdb.client.ZRevRangeWithScores(ctx, key, tart, stop).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache ZRevRange err:%v", err) |
|
return nil, err |
|
} |
|
return result, nil |
|
} |
|
|
|
func (rdb Cache) ZRem(ctx context.Context, key string, number string) error { |
|
_, err := rdb.client.ZRem(ctx, key, number).Result() |
|
if err != nil { |
|
rdb.log.Errorf("Redis cache ZRem err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
func (rdb Cache) ZScore(ctx context.Context, key string, number string) (float64, error) { |
|
result, err := rdb.client.ZScore(ctx, key, number).Result() |
|
if err != nil { |
|
rdb.log.Errorf("Redis cache ZScore err:%v", err) |
|
return 0, err |
|
} |
|
return result, nil |
|
} |
|
|
|
func (rdb Cache) HSet(ctx context.Context, hkey string, key, value interface{}) error { |
|
_, err := rdb.client.HSet(ctx, hkey, key, value).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache hset err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
func (rdb Cache) HSetExp(ctx context.Context, hkey string, key, value interface{}, duration time.Duration) error { |
|
err := rdb.client.HSet(ctx, hkey, key).Err() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache hset Expire err:%v", err) |
|
return err |
|
} |
|
|
|
err = rdb.client.Expire(ctx, hkey, duration).Err() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache hset Expire err:%v", err) |
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
|
|
func (rdb Cache) HGet(ctx context.Context, hkey string, key string) (string, error) { |
|
result, err := rdb.client.HGet(ctx, hkey, key).Result() |
|
if err != nil { |
|
rdb.log.Errorf("redis cache hset Expire err:%v", err) |
|
return "", err |
|
} |
|
return result, err |
|
} |
|
|
|
func (rdb Cache) HKeys(ctx context.Context, hkey string) ([]string, error) { |
|
result, err := rdb.client.HKeys(ctx, hkey).Result() |
|
if err != nil { |
|
rdb.log.Errorf("Get Redis cache hkeys err:%v", err) |
|
return nil, err |
|
} |
|
return result, nil |
|
}
|
|
|