From addd353b2e36e8d1a7a4a4219e2eb7c480098204 Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Mon, 27 Feb 2023 23:49:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8fs.Context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redisHash.go | 23 ++++++++++++----------- redisKey.go | 9 +++++---- redisList.go | 19 ++++++++++--------- redisLock.go | 9 +++++---- redisSet.go | 27 +++++++++++++++------------ redisString.go | 7 ++++--- redisZSet.go | 11 ++++++----- 7 files changed, 57 insertions(+), 48 deletions(-) diff --git a/redisHash.go b/redisHash.go index 76259a6..c1dd62f 100644 --- a/redisHash.go +++ b/redisHash.go @@ -3,6 +3,7 @@ package redis import ( "encoding/json" "github.com/farseer-go/collections" + "github.com/farseer-go/fs" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/types" "github.com/go-redis/redis/v8" @@ -15,25 +16,25 @@ type redisHash struct { func (redisHash *redisHash) HashSetEntity(key string, field string, entity any) error { jsonContent, _ := json.Marshal(entity) - return redisHash.rdb.HSet(ctx, key, field, string(jsonContent)).Err() + return redisHash.rdb.HSet(fs.Context, key, field, string(jsonContent)).Err() } func (redisHash *redisHash) HashSet(key string, fieldValues ...any) error { - return redisHash.rdb.HSet(ctx, key, fieldValues...).Err() + return redisHash.rdb.HSet(fs.Context, key, fieldValues...).Err() } func (redisHash *redisHash) HashGet(key string, field string) (string, error) { - return redisHash.rdb.HGet(ctx, key, field).Result() + return redisHash.rdb.HGet(fs.Context, key, field).Result() } func (redisHash *redisHash) HashGetAll(key string) (map[string]string, error) { - return redisHash.rdb.HGetAll(ctx, key).Result() + return redisHash.rdb.HGetAll(fs.Context, key).Result() } func (redisHash *redisHash) HashToEntity(key string, field string, entity any) (bool, error) { - jsonContent, err := redisHash.rdb.HGet(ctx, key, field).Result() + jsonContent, err := redisHash.rdb.HGet(fs.Context, key, field).Result() if err != nil { - if err.Error() == "redis: nil" { + if err == redis.Nil { return false, nil } return false, err @@ -49,7 +50,7 @@ func (redisHash *redisHash) HashToArray(key string, arrSlice any) error { panic("arr入参必须为切片类型") } - result, err := redisHash.rdb.HGetAll(ctx, key).Result() + result, err := redisHash.rdb.HGetAll(fs.Context, key).Result() if err != nil { return flog.Error(err) } @@ -67,7 +68,7 @@ func (redisHash *redisHash) HashToArray(key string, arrSlice any) error { func (redisHash *redisHash) HashToListAny(key string, itemType reflect.Type) (collections.ListAny, error) { lst := collections.NewListAny() - result, err := redisHash.rdb.HGetAll(ctx, key).Result() + result, err := redisHash.rdb.HGetAll(fs.Context, key).Result() if err != nil { _ = flog.Error(err) return lst, err @@ -81,16 +82,16 @@ func (redisHash *redisHash) HashToListAny(key string, itemType reflect.Type) (co } func (redisHash *redisHash) HashExists(key string, field string) (bool, error) { - return redisHash.rdb.HExists(ctx, key, field).Result() + return redisHash.rdb.HExists(fs.Context, key, field).Result() } func (redisHash *redisHash) HashDel(key string, fields ...string) (bool, error) { - result, err := redisHash.rdb.HDel(ctx, key, fields...).Result() + result, err := redisHash.rdb.HDel(fs.Context, key, fields...).Result() return result > 0, err } func (redisHash *redisHash) HashCount(key string) int { - hLen := redisHash.rdb.HLen(ctx, key) + hLen := redisHash.rdb.HLen(fs.Context, key) count, _ := hLen.Uint64() return int(count) } diff --git a/redisKey.go b/redisKey.go index 3a6683b..373bcbe 100644 --- a/redisKey.go +++ b/redisKey.go @@ -1,6 +1,7 @@ package redis import ( + "github.com/farseer-go/fs" "github.com/go-redis/redis/v8" "time" ) @@ -10,19 +11,19 @@ type redisKey struct { } func (redisKey *redisKey) SetTTL(key string, d time.Duration) (bool, error) { - return redisKey.rdb.Expire(ctx, key, d).Result() + return redisKey.rdb.Expire(fs.Context, key, d).Result() } func (redisKey *redisKey) TTL(key string) (time.Duration, error) { - return redisKey.rdb.TTL(ctx, key).Result() + return redisKey.rdb.TTL(fs.Context, key).Result() } func (redisKey *redisKey) Del(keys ...string) (bool, error) { - result, err := redisKey.rdb.Del(ctx, keys...).Result() + result, err := redisKey.rdb.Del(fs.Context, keys...).Result() return result > 0, err } func (redisKey *redisKey) Exists(keys ...string) (bool, error) { - result, err := redisKey.rdb.Exists(ctx, keys...).Result() + result, err := redisKey.rdb.Exists(fs.Context, keys...).Result() return result > 0, err } diff --git a/redisList.go b/redisList.go index c3f22dd..2e75871 100644 --- a/redisList.go +++ b/redisList.go @@ -1,6 +1,7 @@ package redis import ( + "github.com/farseer-go/fs" "github.com/go-redis/redis/v8" "time" ) @@ -10,17 +11,17 @@ type redisList struct { } func (redisList *redisList) ListPushRight(key string, values ...any) (bool, error) { - result, err := redisList.rdb.RPush(ctx, key, values).Result() + result, err := redisList.rdb.RPush(fs.Context, key, values).Result() return result > 0, err } func (redisList *redisList) ListPushLeft(key string, values ...any) (bool, error) { - result, err := redisList.rdb.LPush(ctx, key, values).Result() + result, err := redisList.rdb.LPush(fs.Context, key, values).Result() return result > 0, err } func (redisList *redisList) ListSet(key string, index int64, value any) (bool, error) { - result, err := redisList.rdb.LSet(ctx, key, index, value).Result() + result, err := redisList.rdb.LSet(fs.Context, key, index, value).Result() if result == "OK" { return true, err } @@ -28,27 +29,27 @@ func (redisList *redisList) ListSet(key string, index int64, value any) (bool, e } func (redisList *redisList) ListRemove(key string, count int64, value any) (bool, error) { - result, err := redisList.rdb.LRem(ctx, key, count, value).Result() + result, err := redisList.rdb.LRem(fs.Context, key, count, value).Result() return result > 0, err } func (redisList *redisList) ListCount(key string) (int64, error) { - return redisList.rdb.LLen(ctx, key).Result() + return redisList.rdb.LLen(fs.Context, key).Result() } func (redisList *redisList) ListRange(key string, start int64, stop int64) ([]string, error) { - return redisList.rdb.LRange(ctx, key, start, stop).Result() + return redisList.rdb.LRange(fs.Context, key, start, stop).Result() } func (redisList *redisList) ListLeftPop(timeout time.Duration, keys ...string) ([]string, error) { - return redisList.rdb.BLPop(ctx, timeout, keys...).Result() + return redisList.rdb.BLPop(fs.Context, timeout, keys...).Result() } func (redisList *redisList) ListRightPop(timeout time.Duration, keys ...string) ([]string, error) { - return redisList.rdb.BRPop(ctx, timeout, keys...).Result() + return redisList.rdb.BRPop(fs.Context, timeout, keys...).Result() } func (redisList *redisList) ListRightPopPush(source, destination string, timeout time.Duration) (string, error) { - return redisList.rdb.BRPopLPush(ctx, source, destination, timeout).Result() + return redisList.rdb.BRPopLPush(fs.Context, source, destination, timeout).Result() } diff --git a/redisLock.go b/redisLock.go index 691fb71..ec7e21e 100644 --- a/redisLock.go +++ b/redisLock.go @@ -1,6 +1,7 @@ package redis import ( + "github.com/farseer-go/fs" "github.com/farseer-go/fs/core" "github.com/farseer-go/fs/flog" "github.com/farseer-go/fs/stopwatch" @@ -30,7 +31,7 @@ func (r redisLock) LockNew(key string, expiration time.Duration) core.ILock { // TryLock 尝试加锁 func (r *lockResult) TryLock() bool { sw := stopwatch.StartNew() - cmd := r.rdb.SetNX(ctx, r.key, 1, r.expiration) + cmd := r.rdb.SetNX(fs.Context, r.key, 1, r.expiration) result, err := cmd.Result() flog.Debugf("获取Redis锁,耗时:%s", sw.GetMicrosecondsText()) if err != nil { @@ -42,7 +43,7 @@ func (r *lockResult) TryLock() bool { // TryLockRun 尝试加锁,执行完后,自动释放锁 func (r *lockResult) TryLockRun(fn func()) bool { sw := stopwatch.StartNew() - cmd := r.rdb.SetNX(ctx, r.key, 1, r.expiration) + cmd := r.rdb.SetNX(fs.Context, r.key, 1, r.expiration) result, err := cmd.Result() flog.Debugf("获取Redis锁,耗时:%s", sw.GetMicrosecondsText()) if err != nil { @@ -59,7 +60,7 @@ func (r *lockResult) TryLockRun(fn func()) bool { func (r *lockResult) GetLock() { sw := stopwatch.StartNew() for { - cmd := r.rdb.SetNX(ctx, r.key, 1, r.expiration) + cmd := r.rdb.SetNX(fs.Context, r.key, 1, r.expiration) result, err := cmd.Result() flog.Debugf("获取Redis锁,耗时:%s", sw.GetMicrosecondsText()) if err != nil { @@ -84,5 +85,5 @@ func (r *lockResult) GetLockRun(fn func()) { // ReleaseLock 锁放锁 func (r *lockResult) ReleaseLock() { - r.rdb.Del(ctx, r.key) + r.rdb.Del(fs.Context, r.key) } diff --git a/redisSet.go b/redisSet.go index 28e6769..1fcc58d 100644 --- a/redisSet.go +++ b/redisSet.go @@ -1,56 +1,59 @@ package redis -import "github.com/go-redis/redis/v8" +import ( + "github.com/farseer-go/fs" + "github.com/go-redis/redis/v8" +) type redisSet struct { rdb *redis.Client } func (redisSet *redisSet) SetAdd(key string, members ...any) (bool, error) { - result, err := redisSet.rdb.SAdd(ctx, key, members...).Result() + result, err := redisSet.rdb.SAdd(fs.Context, key, members...).Result() return result > 0, err } func (redisSet *redisSet) SetCount(key string) (int64, error) { - return redisSet.rdb.SCard(ctx, key).Result() + return redisSet.rdb.SCard(fs.Context, key).Result() } func (redisSet *redisSet) SetRemove(key string, members ...any) (bool, error) { - result, err := redisSet.rdb.SRem(ctx, key, members...).Result() + result, err := redisSet.rdb.SRem(fs.Context, key, members...).Result() return result > 0, err } func (redisSet *redisSet) SetGet(key string) ([]string, error) { - return redisSet.rdb.SMembers(ctx, key).Result() + return redisSet.rdb.SMembers(fs.Context, key).Result() } func (redisSet *redisSet) SetIsMember(key string, member any) (bool, error) { - return redisSet.rdb.SIsMember(ctx, key, member).Result() + return redisSet.rdb.SIsMember(fs.Context, key, member).Result() } func (redisSet *redisSet) SetDiff(keys ...string) ([]string, error) { - return redisSet.rdb.SDiff(ctx, keys...).Result() + return redisSet.rdb.SDiff(fs.Context, keys...).Result() } func (redisSet *redisSet) SetDiffStore(destination string, keys ...string) (bool, error) { - result, err := redisSet.rdb.SDiffStore(ctx, destination, keys...).Result() + result, err := redisSet.rdb.SDiffStore(fs.Context, destination, keys...).Result() return result > 0, err } func (redisSet *redisSet) SetInter(keys ...string) ([]string, error) { - return redisSet.rdb.SInter(ctx, keys...).Result() + return redisSet.rdb.SInter(fs.Context, keys...).Result() } func (redisSet *redisSet) SetInterStore(destination string, keys ...string) (bool, error) { - result, err := redisSet.rdb.SInterStore(ctx, destination, keys...).Result() + result, err := redisSet.rdb.SInterStore(fs.Context, destination, keys...).Result() return result > 0, err } func (redisSet *redisSet) SetUnion(keys ...string) ([]string, error) { - return redisSet.rdb.SUnion(ctx, keys...).Result() + return redisSet.rdb.SUnion(fs.Context, keys...).Result() } func (redisSet *redisSet) SetUnionStore(destination string, keys ...string) (bool, error) { - result, err := redisSet.rdb.SUnionStore(ctx, destination, keys...).Result() + result, err := redisSet.rdb.SUnionStore(fs.Context, destination, keys...).Result() return result > 0, err } diff --git a/redisString.go b/redisString.go index 365e903..807fbd2 100644 --- a/redisString.go +++ b/redisString.go @@ -1,6 +1,7 @@ package redis import ( + "github.com/farseer-go/fs" "github.com/go-redis/redis/v8" "time" ) @@ -11,15 +12,15 @@ type redisString struct { // StringSet 设置缓存 func (redisString *redisString) StringSet(key string, value any) error { - return redisString.rdb.Set(ctx, key, value, 0).Err() + return redisString.rdb.Set(fs.Context, key, value, 0).Err() } // StringGet 获取缓存 func (redisString *redisString) StringGet(key string) (string, error) { - return redisString.rdb.Get(ctx, key).Result() + return redisString.rdb.Get(fs.Context, key).Result() } // StringSetNX 设置过期时间 func (redisString *redisString) StringSetNX(key string, value any, expiration time.Duration) (bool, error) { - return redisString.rdb.SetNX(ctx, key, value, expiration).Result() + return redisString.rdb.SetNX(fs.Context, key, value, expiration).Result() } diff --git a/redisZSet.go b/redisZSet.go index 9fc9040..d9deb5b 100644 --- a/redisZSet.go +++ b/redisZSet.go @@ -1,6 +1,7 @@ package redis import ( + "github.com/farseer-go/fs" "github.com/go-redis/redis/v8" ) @@ -22,23 +23,23 @@ func (redisZSet *redisZSet) ZSetAdd(key string, members ...*redisZ) (bool, error for _, member := range members { redisZZ = append(redisZZ, &redis.Z{Score: member.Score, Member: member.Member}) } - result, err := redisZSet.rdb.ZAdd(ctx, key, redisZZ...).Result() + result, err := redisZSet.rdb.ZAdd(fs.Context, key, redisZZ...).Result() return result > 0, err } func (redisZSet *redisZSet) ZSetScore(key string, member string) (float64, error) { - return redisZSet.rdb.ZScore(ctx, key, member).Result() + return redisZSet.rdb.ZScore(fs.Context, key, member).Result() } func (redisZSet *redisZSet) ZSetRange(key string, start int64, stop int64) ([]string, error) { - return redisZSet.rdb.ZRange(ctx, key, start, stop).Result() + return redisZSet.rdb.ZRange(fs.Context, key, start, stop).Result() } func (redisZSet *redisZSet) ZSetRevRange(key string, start int64, stop int64) ([]string, error) { - return redisZSet.rdb.ZRevRange(ctx, key, start, stop).Result() + return redisZSet.rdb.ZRevRange(fs.Context, key, start, stop).Result() } func (redisZSet *redisZSet) ZSetRangeByScore(key string, opt *redisZRangeBy) ([]string, error) { rby := redis.ZRangeBy{Min: opt.Min, Max: opt.Max, Offset: opt.Offset, Count: opt.Count} - return redisZSet.rdb.ZRangeByScore(ctx, key, &rby).Result() + return redisZSet.rdb.ZRangeByScore(fs.Context, key, &rby).Result() }