From 8e27344f05f0952edc17163ab610277ee9655c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 13 Aug 2022 17:47:33 +0800 Subject: [PATCH] - update redis --- .gitignore | 1 + const.go | 2 +- redis_hash_operation.go | 15 ++++---- redis_lock.go | 66 +++++++++++++++++++++++++++++++++ redis_simple_cache.go | 15 ++++---- redis_simple_interface_cache.go | 13 +++---- redis_simple_json_cache.go | 11 +++--- redis_simple_operation.go | 20 ++++------ redis_simple_sring_cache.go | 11 +++--- redis_string_operation.go | 24 +++++------- 10 files changed, 119 insertions(+), 59 deletions(-) create mode 100644 redis_lock.go diff --git a/.gitignore b/.gitignore index 50df396..79fdf16 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ *.txt *.text gomod.sh +/vendor/ diff --git a/const.go b/const.go index b687808..8e0e220 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package dorm -const Version = "1.0.19" +const Version = "1.0.20" diff --git a/redis_hash_operation.go b/redis_hash_operation.go index 6e29e1d..0b1a51e 100644 --- a/redis_hash_operation.go +++ b/redis_hash_operation.go @@ -6,21 +6,20 @@ import ( ) type HashOperation struct { - db *redis.Client - ctx context.Context + db *redis.Client } // NewHashOperation hash类型数据操作 https://www.tizi365.com/archives/296.html -func NewHashOperation(db *redis.Client, ctx context.Context) *HashOperation { - return &HashOperation{db: db, ctx: ctx} +func NewHashOperation(db *redis.Client) *HashOperation { + return &HashOperation{db: db} } // Set 根据key和field字段设置,field字段的值 -func (cl *HashOperation) Set(key string, value interface{}) *redis.IntCmd { - return cl.db.HSet(cl.ctx, key, value) +func (cl *HashOperation) Set(ctx context.Context, key string, value interface{}) *redis.IntCmd { + return cl.db.HSet(ctx, key, value) } // Get 根据key和field字段设置,field字段的值 -func (cl *HashOperation) Get(key, field string) *redis.StringCmd { - return cl.db.HGet(cl.ctx, key, field) +func (cl *HashOperation) Get(ctx context.Context, key, field string) *redis.StringCmd { + return cl.db.HGet(ctx, key, field) } diff --git a/redis_lock.go b/redis_lock.go new file mode 100644 index 0000000..e61997f --- /dev/null +++ b/redis_lock.go @@ -0,0 +1,66 @@ +package dorm + +import ( + "context" + "errors" + "time" +) + +// RedisClientLock https://github.com/go-redis/redis +type RedisClientLock struct{} + +// NewLock 实例化锁 +func (r *RedisClient) NewLock() *RedisClientLock { + return &RedisClientLock{} +} + +// Lock 上锁 +// key 锁名 +// val 锁内容 +// ttl 锁过期时间 +func (r *RedisClient) Lock(ctx context.Context, key string, val string, ttl time.Duration) (resp string, err error) { + if ttl <= 0 { + return resp, errors.New("长期请使用 LockForever 方法") + } + // 获取 + get, err := r.Get(ctx, key).Result() + if err != nil { + return resp, errors.New("获取异常") + } + if get != "" { + return resp, errors.New("上锁失败,已存在") + } + // 设置 + err = r.Set(ctx, key, val, ttl).Err() + if err != nil { + return resp, errors.New("上锁失败") + } + return val, nil +} + +// Unlock 解锁 +// key 锁名 +func (r *RedisClient) Unlock(ctx context.Context, key string) error { + _, err := r.Del(ctx, key).Result() + return err +} + +// LockForever 永远上锁 +// key 锁名 +// val 锁内容 +func (r *RedisClient) LockForever(ctx context.Context, key string, val string) (resp string, err error) { + // 获取 + get, err := r.Get(ctx, key).Result() + if err != nil { + return resp, errors.New("获取异常") + } + if get != "" { + return resp, errors.New("上锁失败,已存在") + } + // 设置 + err = r.Set(ctx, key, val, 0).Err() + if err != nil { + return resp, errors.New("上锁失败") + } + return val, nil +} diff --git a/redis_simple_cache.go b/redis_simple_cache.go index e760b1b..c0a7df2 100644 --- a/redis_simple_cache.go +++ b/redis_simple_cache.go @@ -1,6 +1,7 @@ package dorm import ( + "context" "encoding/json" "time" ) @@ -33,12 +34,12 @@ func (r *RedisClient) NewSimpleCache(operation *StringOperation, expire time.Dur } // SetCache 设置缓存 -func (c *SimpleCache) SetCache(key string, value interface{}) { - c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +func (c *SimpleCache) SetCache(ctx context.Context, key string, value interface{}) { + c.Operation.Set(ctx, key, value, WithExpire(c.Expire)).Unwrap() } // GetCache 获取缓存 -func (c *SimpleCache) GetCache(key string) (ret interface{}) { +func (c *SimpleCache) GetCache(ctx context.Context, key string) (ret interface{}) { if c.Serializer == SerializerJson { f := func() string { obj := c.JsonGetter() @@ -48,14 +49,14 @@ func (c *SimpleCache) GetCache(key string) (ret interface{}) { } return string(b) } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) + ret = c.Operation.Get(ctx, key).UnwrapOrElse(f) + c.SetCache(ctx, key, ret) } else if c.Serializer == SerializerString { f := func() string { return c.DBGetter() } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) + ret = c.Operation.Get(ctx, key).UnwrapOrElse(f) + c.SetCache(ctx, key, ret) } return } diff --git a/redis_simple_interface_cache.go b/redis_simple_interface_cache.go index 795b492..ba5bd1b 100644 --- a/redis_simple_interface_cache.go +++ b/redis_simple_interface_cache.go @@ -1,7 +1,7 @@ package dorm import ( - "log" + "context" "time" ) @@ -23,17 +23,16 @@ func (r *RedisClient) NewSimpleInterfaceCache(operation *SimpleOperation, expire } // SetCache 设置缓存 -func (c *SimpleInterfaceCache) SetCache(key string, value interface{}) { - c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +func (c *SimpleInterfaceCache) SetCache(ctx context.Context, key string, value interface{}) { + c.Operation.Set(ctx, key, value, WithExpire(c.Expire)).Unwrap() } // GetCache 获取缓存 -func (c *SimpleInterfaceCache) GetCache(key string) (ret interface{}) { +func (c *SimpleInterfaceCache) GetCache(ctx context.Context, key string) (ret interface{}) { f := func() interface{} { return c.DBGetter() } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) - log.Println(ret) + ret = c.Operation.Get(ctx, key).UnwrapOrElse(f) + c.SetCache(ctx, key, ret) return ret } diff --git a/redis_simple_json_cache.go b/redis_simple_json_cache.go index d51f8a6..235958e 100644 --- a/redis_simple_json_cache.go +++ b/redis_simple_json_cache.go @@ -1,6 +1,7 @@ package dorm import ( + "context" "encoding/json" "time" ) @@ -23,12 +24,12 @@ func (r *RedisClient) NewSimpleJsonCache(operation *StringOperation, expire time } // SetCache 设置缓存 -func (c *SimpleJsonCache) SetCache(key string, value interface{}) { - c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +func (c *SimpleJsonCache) SetCache(ctx context.Context, key string, value interface{}) { + c.Operation.Set(ctx, key, value, WithExpire(c.Expire)).Unwrap() } // GetCache 获取缓存 -func (c *SimpleJsonCache) GetCache(key string) (ret interface{}) { +func (c *SimpleJsonCache) GetCache(ctx context.Context, key string) (ret interface{}) { f := func() string { obj := c.DBGetter() b, err := json.Marshal(obj) @@ -37,7 +38,7 @@ func (c *SimpleJsonCache) GetCache(key string) (ret interface{}) { } return string(b) } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) + ret = c.Operation.Get(ctx, key).UnwrapOrElse(f) + c.SetCache(ctx, key, ret) return } diff --git a/redis_simple_operation.go b/redis_simple_operation.go index 661ba46..85fe68d 100644 --- a/redis_simple_operation.go +++ b/redis_simple_operation.go @@ -7,32 +7,28 @@ import ( ) type SimpleOperation struct { - db *redis.Client - ctx context.Context + db *redis.Client } func (r *RedisClient) NewSimpleOperation() *SimpleOperation { - return &SimpleOperation{ - db: r.Db, - ctx: context.Background(), - } + return &SimpleOperation{db: r.Db} } // Set 设置 -func (o *SimpleOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *SimpleResult { +func (o *SimpleOperation) Set(ctx context.Context, key string, value interface{}, attrs ...*OperationAttr) *SimpleResult { exp := OperationAttrs(attrs).Find(AttrExpr) if exp == nil { exp = time.Second * 0 } - return NewSimpleResult(o.db.Set(o.ctx, key, value, exp.(time.Duration)).Result()) + return NewSimpleResult(o.db.Set(ctx, key, value, exp.(time.Duration)).Result()) } // Get 获取单个 -func (o *SimpleOperation) Get(key string) *SimpleResult { - return NewSimpleResult(o.db.Get(o.ctx, key).Result()) +func (o *SimpleOperation) Get(ctx context.Context, key string) *SimpleResult { + return NewSimpleResult(o.db.Get(ctx, key).Result()) } // Del 删除key操作,支持批量删除 -func (o *SimpleOperation) Del(keys ...string) *redis.IntCmd { - return o.db.Del(o.ctx, keys...) +func (o *SimpleOperation) Del(ctx context.Context, keys ...string) *redis.IntCmd { + return o.db.Del(ctx, keys...) } diff --git a/redis_simple_sring_cache.go b/redis_simple_sring_cache.go index 1216ab8..9e73a43 100644 --- a/redis_simple_sring_cache.go +++ b/redis_simple_sring_cache.go @@ -1,6 +1,7 @@ package dorm import ( + "context" "time" ) @@ -22,16 +23,16 @@ func (r *RedisClient) NewSimpleStringCache(operation *StringOperation, expire ti } // SetCache 设置缓存 -func (c *SimpleStringCache) SetCache(key string, value string) { - c.Operation.Set(key, value, WithExpire(c.Expire)).Unwrap() +func (c *SimpleStringCache) SetCache(ctx context.Context, key string, value string) { + c.Operation.Set(ctx, key, value, WithExpire(c.Expire)).Unwrap() } // GetCache 获取缓存 -func (c *SimpleStringCache) GetCache(key string) (ret string) { +func (c *SimpleStringCache) GetCache(ctx context.Context, key string) (ret string) { f := func() string { return c.DBGetter() } - ret = c.Operation.Get(key).UnwrapOrElse(f) - c.SetCache(key, ret) + ret = c.Operation.Get(ctx, key).UnwrapOrElse(f) + c.SetCache(ctx, key, ret) return } diff --git a/redis_string_operation.go b/redis_string_operation.go index c2c7ed3..5384cb1 100644 --- a/redis_string_operation.go +++ b/redis_string_operation.go @@ -7,37 +7,33 @@ import ( ) type StringOperation struct { - db *redis.Client - ctx context.Context + db *redis.Client } func (r *RedisClient) NewStringOperation() *StringOperation { - return &StringOperation{ - db: r.Db, - ctx: context.Background(), - } + return &StringOperation{db: r.Db} } // Set 设置 -func (o *StringOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *StringResult { +func (o *StringOperation) Set(ctx context.Context, key string, value interface{}, attrs ...*OperationAttr) *StringResult { exp := OperationAttrs(attrs).Find(AttrExpr) if exp == nil { exp = time.Second * 0 } - return NewStringResult(o.db.Set(o.ctx, key, value, exp.(time.Duration)).Result()) + return NewStringResult(o.db.Set(ctx, key, value, exp.(time.Duration)).Result()) } // Get 获取单个 -func (o *StringOperation) Get(key string) *StringResult { - return NewStringResult(o.db.Get(o.ctx, key).Result()) +func (o *StringOperation) Get(ctx context.Context, key string) *StringResult { + return NewStringResult(o.db.Get(ctx, key).Result()) } // MGet 获取多个 -func (o *StringOperation) MGet(keys ...string) *SliceResult { - return NewSliceResult(o.db.MGet(o.ctx, keys...).Result()) +func (o *StringOperation) MGet(ctx context.Context, keys ...string) *SliceResult { + return NewSliceResult(o.db.MGet(ctx, keys...).Result()) } // Del 删除key操作,支持批量删除 -func (o *StringOperation) Del(keys ...string) *redis.IntCmd { - return o.db.Del(o.ctx, keys...) +func (o *StringOperation) Del(ctx context.Context, keys ...string) *redis.IntCmd { + return o.db.Del(ctx, keys...) }