- update redis
continuous-integration/drone/tag Build is failing Details
continuous-integration/drone/push Build was killed Details

master v1.0.20
李光春 2 years ago
parent 75a6f160b9
commit 8e27344f05

1
.gitignore vendored

@ -8,3 +8,4 @@
*.txt
*.text
gomod.sh
/vendor/

@ -1,3 +1,3 @@
package dorm
const Version = "1.0.19"
const Version = "1.0.20"

@ -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)
}

@ -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
}

@ -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
}

@ -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
}

@ -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
}

@ -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...)
}

@ -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
}

@ -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...)
}

Loading…
Cancel
Save