Skip to content

Commit

Permalink
统一使用fs.Context
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Feb 27, 2023
1 parent 48c15f2 commit addd353
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 48 deletions.
23 changes: 12 additions & 11 deletions redisHash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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)
}
9 changes: 5 additions & 4 deletions redisKey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"github.com/farseer-go/fs"
"github.com/go-redis/redis/v8"
"time"
)
Expand All @@ -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
}
19 changes: 10 additions & 9 deletions redisList.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"github.com/farseer-go/fs"
"github.com/go-redis/redis/v8"
"time"
)
Expand All @@ -10,45 +11,45 @@ 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
}
return false, err
}

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()
}
9 changes: 5 additions & 4 deletions redisLock.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
27 changes: 15 additions & 12 deletions redisSet.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 4 additions & 3 deletions redisString.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"github.com/farseer-go/fs"
"github.com/go-redis/redis/v8"
"time"
)
Expand All @@ -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()
}
11 changes: 6 additions & 5 deletions redisZSet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"github.com/farseer-go/fs"
"github.com/go-redis/redis/v8"
)

Expand All @@ -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()
}

0 comments on commit addd353

Please sign in to comment.