Skip to content

Commit

Permalink
fix #3191 (#3194)
Browse files Browse the repository at this point in the history
  • Loading branch information
hailaz committed Dec 28, 2023
1 parent 984cca8 commit 63bdf41
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 174 deletions.
39 changes: 5 additions & 34 deletions contrib/nosql/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@
package redis

import (
"context"
"crypto/tls"
"time"

"github.com/redis/go-redis/v9"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
)

// Redis is an implement of Adapter using go-redis.
type Redis struct {
gredis.AdapterOperation

client redis.UniversalClient
config *gredis.Config
}
Expand Down Expand Up @@ -75,40 +74,12 @@ func New(config *gredis.Config) *Redis {
client = redis.NewClient(opts.Simple())
}

return &Redis{
r := &Redis{
client: client,
config: config,
}
}

// Do send a command to the server and returns the received reply.
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
conn, err := r.Conn(ctx)
if err != nil {
return nil, err
}
defer func() {
_ = conn.Close(ctx)
}()
return conn.Do(ctx, command, args...)
}

// Close closes the redis connection pool, which will release all connections reserved by this pool.
// It is commonly not necessary to call Close manually.
func (r *Redis) Close(ctx context.Context) (err error) {
if err = r.client.Close(); err != nil {
err = gerror.Wrap(err, `Redis Client Close failed`)
}
return
}

// Conn retrieves and returns a connection object for continuous operations.
// Note that you should call Close function manually if you do not use this connection any further.
func (r *Redis) Conn(ctx context.Context) (gredis.Conn, error) {
return &Conn{
redis: r,
}, nil
r.AdapterOperation = r
return r
}

func fillWithDefaultConfiguration(config *gredis.Config) {
Expand Down
48 changes: 24 additions & 24 deletions contrib/nosql/redis/redis_group_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (

// GroupGeneric provides generic functions of redis.
type GroupGeneric struct {
redis *Redis
Operation gredis.AdapterOperation
}

// GroupGeneric creates and returns GroupGeneric.
func (r *Redis) GroupGeneric() gredis.IGroupGeneric {
return GroupGeneric{
redis: r,
Operation: r.AdapterOperation,
}
}

Expand All @@ -45,7 +45,7 @@ func (r GroupGeneric) Copy(ctx context.Context, source, destination string, opti
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "Copy", mustMergeOptionToArgs(
v, err := r.Operation.Do(ctx, "Copy", mustMergeOptionToArgs(
[]interface{}{source, destination}, usedOption,
)...)
return v.Int64(), err
Expand All @@ -59,7 +59,7 @@ func (r GroupGeneric) Copy(ctx context.Context, source, destination string, opti
//
// https://redis.io/commands/exists/
func (r GroupGeneric) Exists(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "Exists", gconv.Interfaces(keys)...)
v, err := r.Operation.Do(ctx, "Exists", gconv.Interfaces(keys)...)
return v.Int64(), err
}

Expand All @@ -70,7 +70,7 @@ func (r GroupGeneric) Exists(ctx context.Context, keys ...string) (int64, error)
//
// https://redis.io/commands/type/
func (r GroupGeneric) Type(ctx context.Context, key string) (string, error) {
v, err := r.redis.Do(ctx, "Type", key)
v, err := r.Operation.Do(ctx, "Type", key)
return v.String(), err
}

Expand All @@ -83,7 +83,7 @@ func (r GroupGeneric) Type(ctx context.Context, key string) (string, error) {
//
// https://redis.io/commands/unlink/
func (r GroupGeneric) Unlink(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "Unlink", gconv.Interfaces(keys)...)
v, err := r.Operation.Do(ctx, "Unlink", gconv.Interfaces(keys)...)
return v.Int64(), err
}

Expand All @@ -96,7 +96,7 @@ func (r GroupGeneric) Unlink(ctx context.Context, keys ...string) (int64, error)
//
// https://redis.io/commands/rename/
func (r GroupGeneric) Rename(ctx context.Context, key, newKey string) error {
_, err := r.redis.Do(ctx, "Rename", key, newKey)
_, err := r.Operation.Do(ctx, "Rename", key, newKey)
return err
}

Expand All @@ -111,7 +111,7 @@ func (r GroupGeneric) Rename(ctx context.Context, key, newKey string) error {
//
// https://redis.io/commands/renamenx/
func (r GroupGeneric) RenameNX(ctx context.Context, key, newKey string) (int64, error) {
v, err := r.redis.Do(ctx, "RenameNX", key, newKey)
v, err := r.Operation.Do(ctx, "RenameNX", key, newKey)
return v.Int64(), err
}

Expand All @@ -126,7 +126,7 @@ func (r GroupGeneric) RenameNX(ctx context.Context, key, newKey string) (int64,
//
// https://redis.io/commands/move/
func (r GroupGeneric) Move(ctx context.Context, key string, db int) (int64, error) {
v, err := r.redis.Do(ctx, "Move", key, db)
v, err := r.Operation.Do(ctx, "Move", key, db)
return v.Int64(), err
}

Expand All @@ -137,7 +137,7 @@ func (r GroupGeneric) Move(ctx context.Context, key string, db int) (int64, erro
//
// https://redis.io/commands/del/
func (r GroupGeneric) Del(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "Del", gconv.Interfaces(keys)...)
v, err := r.Operation.Do(ctx, "Del", gconv.Interfaces(keys)...)
return v.Int64(), err
}

Expand All @@ -147,15 +147,15 @@ func (r GroupGeneric) Del(ctx context.Context, keys ...string) (int64, error) {
//
// https://redis.io/commands/randomkey/
func (r GroupGeneric) RandomKey(ctx context.Context) (string, error) {
v, err := r.redis.Do(ctx, "RandomKey")
v, err := r.Operation.Do(ctx, "RandomKey")
return v.String(), err
}

// DBSize return the number of keys in the currently-selected database.
//
// https://redis.io/commands/dbsize/
func (r GroupGeneric) DBSize(ctx context.Context) (int64, error) {
v, err := r.redis.Do(ctx, "DBSize")
v, err := r.Operation.Do(ctx, "DBSize")
return v.Int64(), err
}

Expand All @@ -166,15 +166,15 @@ func (r GroupGeneric) DBSize(ctx context.Context) (int64, error) {
//
// https://redis.io/commands/keys/
func (r GroupGeneric) Keys(ctx context.Context, pattern string) ([]string, error) {
v, err := r.redis.Do(ctx, "Keys", pattern)
v, err := r.Operation.Do(ctx, "Keys", pattern)
return v.Strings(), err
}

// FlushDB delete all the keys of the currently selected DB. This command never fails.
//
// https://redis.io/commands/flushdb/
func (r GroupGeneric) FlushDB(ctx context.Context, option ...gredis.FlushOp) error {
_, err := r.redis.Do(ctx, "FlushDB", gconv.Interfaces(option)...)
_, err := r.Operation.Do(ctx, "FlushDB", gconv.Interfaces(option)...)
return err
}

Expand All @@ -191,7 +191,7 @@ func (r GroupGeneric) FlushDB(ctx context.Context, option ...gredis.FlushOp) err
//
// https://redis.io/commands/flushall/
func (r GroupGeneric) FlushAll(ctx context.Context, option ...gredis.FlushOp) error {
_, err := r.redis.Do(ctx, "FlushAll", gconv.Interfaces(option)...)
_, err := r.Operation.Do(ctx, "FlushAll", gconv.Interfaces(option)...)
return err
}

Expand All @@ -208,7 +208,7 @@ func (r GroupGeneric) Expire(ctx context.Context, key string, seconds int64, opt
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "Expire", mustMergeOptionToArgs(
v, err := r.Operation.Do(ctx, "Expire", mustMergeOptionToArgs(
[]interface{}{key, seconds}, usedOption,
)...)
return v.Int64(), err
Expand All @@ -229,7 +229,7 @@ func (r GroupGeneric) ExpireAt(ctx context.Context, key string, time time.Time,
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "ExpireAt", mustMergeOptionToArgs(
v, err := r.Operation.Do(ctx, "ExpireAt", mustMergeOptionToArgs(
[]interface{}{key, gtime.New(time).Timestamp()}, usedOption,
)...)
return v.Int64(), err
Expand All @@ -243,7 +243,7 @@ func (r GroupGeneric) ExpireAt(ctx context.Context, key string, time time.Time,
//
// https://redis.io/commands/expiretime/
func (r GroupGeneric) ExpireTime(ctx context.Context, key string) (*gvar.Var, error) {
return r.redis.Do(ctx, "ExpireTime", key)
return r.Operation.Do(ctx, "ExpireTime", key)
}

// TTL returns the remaining time to live of a key that has a timeout.
Expand All @@ -263,7 +263,7 @@ func (r GroupGeneric) ExpireTime(ctx context.Context, key string) (*gvar.Var, er
//
// https://redis.io/commands/ttl/
func (r GroupGeneric) TTL(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "TTL", key)
v, err := r.Operation.Do(ctx, "TTL", key)
return v.Int64(), err
}

Expand All @@ -276,7 +276,7 @@ func (r GroupGeneric) TTL(ctx context.Context, key string) (int64, error) {
//
// https://redis.io/commands/persist/
func (r GroupGeneric) Persist(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "Persist", key)
v, err := r.Operation.Do(ctx, "Persist", key)
return v.Int64(), err
}

Expand All @@ -293,7 +293,7 @@ func (r GroupGeneric) PExpire(ctx context.Context, key string, milliseconds int6
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "PExpire", mustMergeOptionToArgs(
v, err := r.Operation.Do(ctx, "PExpire", mustMergeOptionToArgs(
[]interface{}{key, milliseconds}, usedOption,
)...)
return v.Int64(), err
Expand All @@ -308,7 +308,7 @@ func (r GroupGeneric) PExpireAt(ctx context.Context, key string, time time.Time,
if len(option) > 0 {
usedOption = option[0]
}
v, err := r.redis.Do(ctx, "PExpireAt", mustMergeOptionToArgs(
v, err := r.Operation.Do(ctx, "PExpireAt", mustMergeOptionToArgs(
[]interface{}{key, gtime.New(time).TimestampMilli()}, usedOption,
)...)
return v.Int64(), err
Expand All @@ -322,7 +322,7 @@ func (r GroupGeneric) PExpireAt(ctx context.Context, key string, time time.Time,
//
// https://redis.io/commands/pexpiretime/
func (r GroupGeneric) PExpireTime(ctx context.Context, key string) (*gvar.Var, error) {
return r.redis.Do(ctx, "PExpireTime", key)
return r.Operation.Do(ctx, "PExpireTime", key)
}

// PTTL like TTL this command returns the remaining time to live of a key that has an expired set,
Expand All @@ -336,6 +336,6 @@ func (r GroupGeneric) PExpireTime(ctx context.Context, key string) (*gvar.Var, e
//
// https://redis.io/commands/pttl/
func (r GroupGeneric) PTTL(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "PTTL", key)
v, err := r.Operation.Do(ctx, "PTTL", key)
return v.Int64(), err
}

0 comments on commit 63bdf41

Please sign in to comment.