diff --git a/contrib/nosql/redis/redis.go b/contrib/nosql/redis/redis.go index 5240bd8fd27..248624334d0 100644 --- a/contrib/nosql/redis/redis.go +++ b/contrib/nosql/redis/redis.go @@ -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 } @@ -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) { diff --git a/contrib/nosql/redis/redis_group_generic.go b/contrib/nosql/redis/redis_group_generic.go index bac0c94eaf9..cdb11256bf5 100644 --- a/contrib/nosql/redis/redis_group_generic.go +++ b/contrib/nosql/redis/redis_group_generic.go @@ -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, } } @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -147,7 +147,7 @@ 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 } @@ -155,7 +155,7 @@ func (r GroupGeneric) RandomKey(ctx context.Context) (string, error) { // // 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 } @@ -166,7 +166,7 @@ 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 } @@ -174,7 +174,7 @@ func (r GroupGeneric) Keys(ctx context.Context, pattern string) ([]string, error // // 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 } @@ -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 } @@ -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 @@ -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 @@ -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. @@ -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 } @@ -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 } @@ -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 @@ -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 @@ -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, @@ -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 } diff --git a/contrib/nosql/redis/redis_group_hash.go b/contrib/nosql/redis/redis_group_hash.go index 7967321290e..75b3147430c 100644 --- a/contrib/nosql/redis/redis_group_hash.go +++ b/contrib/nosql/redis/redis_group_hash.go @@ -16,13 +16,13 @@ import ( // GroupHash is the redis group object for hash operations. type GroupHash struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupHash creates and returns a redis group object for hash operations. func (r *Redis) GroupHash() gredis.IGroupHash { return GroupHash{ - redis: r, + Operation: r.AdapterOperation, } } @@ -38,7 +38,7 @@ func (r GroupHash) HSet(ctx context.Context, key string, fields map[string]inter for k, v := range fields { s = append(s, k, v) } - v, err := r.redis.Do(ctx, "HSet", s...) + v, err := r.Operation.Do(ctx, "HSet", s...) return v.Int64(), err } @@ -52,7 +52,7 @@ func (r GroupHash) HSet(ctx context.Context, key string, fields map[string]inter // // https://redis.io/commands/hsetnx/ func (r GroupHash) HSetNX(ctx context.Context, key, field string, value interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "HSetNX", key, field, value) + v, err := r.Operation.Do(ctx, "HSetNX", key, field, value) return v.Int64(), err } @@ -62,7 +62,7 @@ func (r GroupHash) HSetNX(ctx context.Context, key, field string, value interfac // // https://redis.io/commands/hget/ func (r GroupHash) HGet(ctx context.Context, key, field string) (*gvar.Var, error) { - v, err := r.redis.Do(ctx, "HGet", key, field) + v, err := r.Operation.Do(ctx, "HGet", key, field) return v, err } @@ -74,7 +74,7 @@ func (r GroupHash) HGet(ctx context.Context, key, field string) (*gvar.Var, erro // // https://redis.io/commands/hstrlen/ func (r GroupHash) HStrLen(ctx context.Context, key, field string) (int64, error) { - v, err := r.redis.Do(ctx, "HSTRLEN", key, field) + v, err := r.Operation.Do(ctx, "HSTRLEN", key, field) return v.Int64(), err } @@ -86,7 +86,7 @@ func (r GroupHash) HStrLen(ctx context.Context, key, field string) (int64, error // // https://redis.io/commands/hexists/ func (r GroupHash) HExists(ctx context.Context, key, field string) (int64, error) { - v, err := r.redis.Do(ctx, "HExists", key, field) + v, err := r.Operation.Do(ctx, "HExists", key, field) return v.Int64(), err } @@ -98,7 +98,7 @@ func (r GroupHash) HExists(ctx context.Context, key, field string) (int64, error // // https://redis.io/commands/hdel/ func (r GroupHash) HDel(ctx context.Context, key string, fields ...string) (int64, error) { - v, err := r.redis.Do(ctx, "HDel", append([]interface{}{key}, gconv.Interfaces(fields)...)...) + v, err := r.Operation.Do(ctx, "HDel", append([]interface{}{key}, gconv.Interfaces(fields)...)...) return v.Int64(), err } @@ -106,7 +106,7 @@ func (r GroupHash) HDel(ctx context.Context, key string, fields ...string) (int6 // // https://redis.io/commands/hlen/ func (r GroupHash) HLen(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "HLen", key) + v, err := r.Operation.Do(ctx, "HLen", key) return v.Int64(), err } @@ -118,7 +118,7 @@ func (r GroupHash) HLen(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/hincrby/ func (r GroupHash) HIncrBy(ctx context.Context, key, field string, increment int64) (int64, error) { - v, err := r.redis.Do(ctx, "HIncrBy", key, field, increment) + v, err := r.Operation.Do(ctx, "HIncrBy", key, field, increment) return v.Int64(), err } @@ -138,7 +138,7 @@ func (r GroupHash) HIncrBy(ctx context.Context, key, field string, increment int // // https://redis.io/commands/hincrbyfloat/ func (r GroupHash) HIncrByFloat(ctx context.Context, key, field string, increment float64) (float64, error) { - v, err := r.redis.Do(ctx, "HIncrByFloat", key, field, increment) + v, err := r.Operation.Do(ctx, "HIncrByFloat", key, field, increment) return v.Float64(), err } @@ -152,7 +152,7 @@ func (r GroupHash) HMSet(ctx context.Context, key string, fields map[string]inte for k, v := range fields { s = append(s, k, v) } - _, err := r.redis.Do(ctx, "HMSet", s...) + _, err := r.Operation.Do(ctx, "HMSet", s...) return err } @@ -163,7 +163,7 @@ func (r GroupHash) HMSet(ctx context.Context, key string, fields map[string]inte // // https://redis.io/commands/hmget/ func (r GroupHash) HMGet(ctx context.Context, key string, fields ...string) (gvar.Vars, error) { - v, err := r.redis.Do(ctx, "HMGet", append([]interface{}{key}, gconv.Interfaces(fields)...)...) + v, err := r.Operation.Do(ctx, "HMGet", append([]interface{}{key}, gconv.Interfaces(fields)...)...) return v.Vars(), err } @@ -171,7 +171,7 @@ func (r GroupHash) HMGet(ctx context.Context, key string, fields ...string) (gva // // https://redis.io/commands/hkeys/ func (r GroupHash) HKeys(ctx context.Context, key string) ([]string, error) { - v, err := r.redis.Do(ctx, "HKeys", key) + v, err := r.Operation.Do(ctx, "HKeys", key) return v.Strings(), err } @@ -179,7 +179,7 @@ func (r GroupHash) HKeys(ctx context.Context, key string) ([]string, error) { // // https://redis.io/commands/hvals/ func (r GroupHash) HVals(ctx context.Context, key string) (gvar.Vars, error) { - v, err := r.redis.Do(ctx, "HVals", key) + v, err := r.Operation.Do(ctx, "HVals", key) return v.Vars(), err } @@ -189,6 +189,6 @@ func (r GroupHash) HVals(ctx context.Context, key string) (gvar.Vars, error) { // // https://redis.io/commands/hgetall/ func (r GroupHash) HGetAll(ctx context.Context, key string) (*gvar.Var, error) { - v, err := r.redis.Do(ctx, "HGetAll", key) + v, err := r.Operation.Do(ctx, "HGetAll", key) return v, err } diff --git a/contrib/nosql/redis/redis_group_list.go b/contrib/nosql/redis/redis_group_list.go index 829b5d84a7a..6767e723fbe 100644 --- a/contrib/nosql/redis/redis_group_list.go +++ b/contrib/nosql/redis/redis_group_list.go @@ -16,13 +16,13 @@ import ( // GroupList is the redis group list object. type GroupList struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupList creates and returns a redis group object for list operations. func (r *Redis) GroupList() gredis.IGroupList { return GroupList{ - redis: r, + Operation: r.AdapterOperation, } } @@ -35,7 +35,7 @@ func (r *Redis) GroupList() gredis.IGroupList { // // https://redis.io/commands/lpush/ func (r GroupList) LPush(ctx context.Context, key string, values ...interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "LPush", append([]interface{}{key}, values...)...) + v, err := r.Operation.Do(ctx, "LPush", append([]interface{}{key}, values...)...) return v.Int64(), err } @@ -48,7 +48,7 @@ func (r GroupList) LPush(ctx context.Context, key string, values ...interface{}) // // https://redis.io/commands/lpushx func (r GroupList) LPushX(ctx context.Context, key string, element interface{}, elements ...interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "LPushX", append([]interface{}{key, element}, elements...)...) + v, err := r.Operation.Do(ctx, "LPushX", append([]interface{}{key, element}, elements...)...) return v.Int64(), err } @@ -67,7 +67,7 @@ func (r GroupList) LPushX(ctx context.Context, key string, element interface{}, // // https://redis.io/commands/rpush func (r GroupList) RPush(ctx context.Context, key string, values ...interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "RPush", append([]interface{}{key}, values...)...) + v, err := r.Operation.Do(ctx, "RPush", append([]interface{}{key}, values...)...) return v.Int64(), err } @@ -81,7 +81,7 @@ func (r GroupList) RPush(ctx context.Context, key string, values ...interface{}) // // https://redis.io/commands/rpushx func (r GroupList) RPushX(ctx context.Context, key string, value interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "RPushX", key, value) + v, err := r.Operation.Do(ctx, "RPushX", key, value) return v.Int64(), err } @@ -103,9 +103,9 @@ func (r GroupList) RPushX(ctx context.Context, key string, value interface{}) (i // https://redis.io/commands/lpop func (r GroupList) LPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) { if len(count) > 0 { - return r.redis.Do(ctx, "LPop", key, count[0]) + return r.Operation.Do(ctx, "LPop", key, count[0]) } - return r.redis.Do(ctx, "LPop", key) + return r.Operation.Do(ctx, "LPop", key) } // RPop remove and returns the last element of the list stored at key. @@ -126,9 +126,9 @@ func (r GroupList) LPop(ctx context.Context, key string, count ...int) (*gvar.Va // https://redis.io/commands/rpop func (r GroupList) RPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) { if len(count) > 0 { - return r.redis.Do(ctx, "RPop", key, count[0]) + return r.Operation.Do(ctx, "RPop", key, count[0]) } - return r.redis.Do(ctx, "RPop", key) + return r.Operation.Do(ctx, "RPop", key) } // LRem removes the first count occurrences of elements equal to value from the list stored at key. @@ -137,7 +137,7 @@ func (r GroupList) RPop(ctx context.Context, key string, count ...int) (*gvar.Va // // https://redis.io/commands/lrem/ func (r GroupList) LRem(ctx context.Context, key string, count int64, value interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "LRem", key, count, value) + v, err := r.Operation.Do(ctx, "LRem", key, count, value) return v.Int64(), err } @@ -148,7 +148,7 @@ func (r GroupList) LRem(ctx context.Context, key string, count int64, value inte // // https://redis.io/commands/llen func (r GroupList) LLen(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "LLen", key) + v, err := r.Operation.Do(ctx, "LLen", key) return v.Int64(), err } @@ -163,7 +163,7 @@ func (r GroupList) LLen(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/lindex func (r GroupList) LIndex(ctx context.Context, key string, index int64) (*gvar.Var, error) { - return r.redis.Do(ctx, "LIndex", key, index) + return r.Operation.Do(ctx, "LIndex", key, index) } // LInsert inserts element in the list stored at key either before or after the reference value pivot. @@ -174,7 +174,7 @@ func (r GroupList) LIndex(ctx context.Context, key string, index int64) (*gvar.V // // https://redis.io/commands/linsert/ func (r GroupList) LInsert(ctx context.Context, key string, op gredis.LInsertOp, pivot, value interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "LInsert", key, string(op), pivot, value) + v, err := r.Operation.Do(ctx, "LInsert", key, string(op), pivot, value) return v.Int64(), err } @@ -184,7 +184,7 @@ func (r GroupList) LInsert(ctx context.Context, key string, op gredis.LInsertOp, // // https://redis.io/commands/lset/ func (r GroupList) LSet(ctx context.Context, key string, index int64, value interface{}) (*gvar.Var, error) { - return r.redis.Do(ctx, "LSet", key, index, value) + return r.Operation.Do(ctx, "LSet", key, index, value) } // LRange returns the specified elements of the list stored at key. @@ -196,7 +196,7 @@ func (r GroupList) LSet(ctx context.Context, key string, index int64, value inte // // https://redis.io/commands/lrange/ func (r GroupList) LRange(ctx context.Context, key string, start, stop int64) (gvar.Vars, error) { - v, err := r.redis.Do(ctx, "LRange", key, start, stop) + v, err := r.Operation.Do(ctx, "LRange", key, start, stop) return v.Vars(), err } @@ -206,7 +206,7 @@ func (r GroupList) LRange(ctx context.Context, key string, start, stop int64) (g // // https://redis.io/commands/ltrim/ func (r GroupList) LTrim(ctx context.Context, key string, start, stop int64) error { - _, err := r.redis.Do(ctx, "LTrim", key, start, stop) + _, err := r.Operation.Do(ctx, "LTrim", key, start, stop) return err } @@ -221,7 +221,7 @@ func (r GroupList) LTrim(ctx context.Context, key string, start, stop int64) err // // https://redis.io/commands/blpop/ func (r GroupList) BLPop(ctx context.Context, timeout int64, keys ...string) (gvar.Vars, error) { - v, err := r.redis.Do(ctx, "BLPop", append(gconv.Interfaces(keys), timeout)...) + v, err := r.Operation.Do(ctx, "BLPop", append(gconv.Interfaces(keys), timeout)...) return v.Vars(), err } @@ -235,7 +235,7 @@ func (r GroupList) BLPop(ctx context.Context, timeout int64, keys ...string) (gv // // https://redis.io/commands/brpop/ func (r GroupList) BRPop(ctx context.Context, timeout int64, keys ...string) (gvar.Vars, error) { - v, err := r.redis.Do(ctx, "BRPop", append(gconv.Interfaces(keys), timeout)...) + v, err := r.Operation.Do(ctx, "BRPop", append(gconv.Interfaces(keys), timeout)...) return v.Vars(), err } @@ -244,7 +244,7 @@ func (r GroupList) BRPop(ctx context.Context, timeout int64, keys ...string) (gv // // https://redis.io/commands/rpoplpush/ func (r GroupList) RPopLPush(ctx context.Context, source, destination string) (*gvar.Var, error) { - return r.redis.Do(ctx, "RPopLPush", source, destination) + return r.Operation.Do(ctx, "RPopLPush", source, destination) } // BRPopLPush is the blocking variant of RPopLPush. @@ -259,5 +259,5 @@ func (r GroupList) RPopLPush(ctx context.Context, source, destination string) (* // // https://redis.io/commands/brpoplpush/ func (r GroupList) BRPopLPush(ctx context.Context, source, destination string, timeout int64) (*gvar.Var, error) { - return r.redis.Do(ctx, "BRPopLPush", source, destination, timeout) + return r.Operation.Do(ctx, "BRPopLPush", source, destination, timeout) } diff --git a/contrib/nosql/redis/redis_group_pubsub.go b/contrib/nosql/redis/redis_group_pubsub.go index bef48b27436..1badd05e179 100644 --- a/contrib/nosql/redis/redis_group_pubsub.go +++ b/contrib/nosql/redis/redis_group_pubsub.go @@ -14,13 +14,13 @@ import ( // GroupPubSub provides pub/sub functions for redis. type GroupPubSub struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupPubSub creates and returns GroupPubSub. func (r *Redis) GroupPubSub() gredis.IGroupPubSub { return GroupPubSub{ - redis: r, + Operation: r.AdapterOperation, } } @@ -36,7 +36,7 @@ func (r *Redis) GroupPubSub() gredis.IGroupPubSub { // // https://redis.io/commands/publish/ func (r GroupPubSub) Publish(ctx context.Context, channel string, message interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "Publish", channel, message) + v, err := r.Operation.Do(ctx, "Publish", channel, message) return v.Int64(), err } @@ -46,7 +46,7 @@ func (r GroupPubSub) Publish(ctx context.Context, channel string, message interf func (r GroupPubSub) Subscribe( ctx context.Context, channel string, channels ...string, ) (gredis.Conn, []*gredis.Subscription, error) { - conn, err := r.redis.Conn(ctx) + conn, err := r.Operation.Conn(ctx) if err != nil { return nil, nil, err } @@ -70,7 +70,7 @@ func (r GroupPubSub) Subscribe( func (r GroupPubSub) PSubscribe( ctx context.Context, pattern string, patterns ...string, ) (gredis.Conn, []*gredis.Subscription, error) { - conn, err := r.redis.Conn(ctx) + conn, err := r.Operation.Conn(ctx) if err != nil { return nil, nil, err } diff --git a/contrib/nosql/redis/redis_group_script.go b/contrib/nosql/redis/redis_group_script.go index 8a38309cca2..a074748dfa9 100644 --- a/contrib/nosql/redis/redis_group_script.go +++ b/contrib/nosql/redis/redis_group_script.go @@ -16,13 +16,13 @@ import ( // GroupScript provides script functions for redis. type GroupScript struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupScript creates and returns GroupScript. func (r *Redis) GroupScript() gredis.IGroupScript { return GroupScript{ - redis: r, + Operation: r.AdapterOperation, } } @@ -33,7 +33,7 @@ func (r GroupScript) Eval(ctx context.Context, script string, numKeys int64, key var s = []interface{}{script, numKeys} s = append(s, gconv.Interfaces(keys)...) s = append(s, args...) - v, err := r.redis.Do(ctx, "Eval", s...) + v, err := r.Operation.Do(ctx, "Eval", s...) return v, err } @@ -47,7 +47,7 @@ func (r GroupScript) EvalSha(ctx context.Context, sha1 string, numKeys int64, ke var s = []interface{}{sha1, numKeys} s = append(s, gconv.Interfaces(keys)...) s = append(s, args...) - v, err := r.redis.Do(ctx, "EvalSha", s...) + v, err := r.Operation.Do(ctx, "EvalSha", s...) return v, err } @@ -57,7 +57,7 @@ func (r GroupScript) EvalSha(ctx context.Context, sha1 string, numKeys int64, ke // // https://redis.io/commands/script-load/ func (r GroupScript) ScriptLoad(ctx context.Context, script string) (string, error) { - v, err := r.redis.Do(ctx, "Script", "Load", script) + v, err := r.Operation.Do(ctx, "Script", "Load", script) return v.String(), err } @@ -75,7 +75,7 @@ func (r GroupScript) ScriptExists(ctx context.Context, sha1 string, sha1s ...str ) s = append(s, "Exists") s = append(s, sha1Array...) - result, err := r.redis.Do(ctx, "Script", s...) + result, err := r.Operation.Do(ctx, "Script", s...) var ( m = make(map[string]bool) resultArray = result.Vars() @@ -99,7 +99,7 @@ func (r GroupScript) ScriptFlush(ctx context.Context, option ...gredis.ScriptFlu s = append(s, mustMergeOptionToArgs( []interface{}{}, usedOption, )...) - _, err := r.redis.Do(ctx, "Script", s...) + _, err := r.Operation.Do(ctx, "Script", s...) return err } @@ -108,6 +108,6 @@ func (r GroupScript) ScriptFlush(ctx context.Context, option ...gredis.ScriptFlu // // https://redis.io/commands/script-kill/ func (r GroupScript) ScriptKill(ctx context.Context) error { - _, err := r.redis.Do(ctx, "Script", "Kill") + _, err := r.Operation.Do(ctx, "Script", "Kill") return err } diff --git a/contrib/nosql/redis/redis_group_set.go b/contrib/nosql/redis/redis_group_set.go index a46bd457eab..631f2d9ff07 100644 --- a/contrib/nosql/redis/redis_group_set.go +++ b/contrib/nosql/redis/redis_group_set.go @@ -16,13 +16,13 @@ import ( // GroupSet provides set functions for redis. type GroupSet struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupSet creates and returns GroupSet. func (r *Redis) GroupSet() gredis.IGroupSet { return GroupSet{ - redis: r, + Operation: r.AdapterOperation, } } @@ -40,7 +40,7 @@ func (r GroupSet) SAdd(ctx context.Context, key string, member interface{}, memb var s = []interface{}{key} s = append(s, member) s = append(s, members...) - v, err := r.redis.Do(ctx, "SAdd", s...) + v, err := r.Operation.Do(ctx, "SAdd", s...) return v.Int64(), err } @@ -52,7 +52,7 @@ func (r GroupSet) SAdd(ctx context.Context, key string, member interface{}, memb // // https://redis.io/commands/sismember/ func (r GroupSet) SIsMember(ctx context.Context, key string, member interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "SIsMember", key, member) + v, err := r.Operation.Do(ctx, "SIsMember", key, member) return v.Int64(), err } @@ -73,7 +73,7 @@ func (r GroupSet) SIsMember(ctx context.Context, key string, member interface{}) func (r GroupSet) SPop(ctx context.Context, key string, count ...int) (*gvar.Var, error) { var s = []interface{}{key} s = append(s, gconv.Interfaces(count)...) - v, err := r.redis.Do(ctx, "SPop", s...) + v, err := r.Operation.Do(ctx, "SPop", s...) return v, err } @@ -95,7 +95,7 @@ func (r GroupSet) SPop(ctx context.Context, key string, count ...int) (*gvar.Var func (r GroupSet) SRandMember(ctx context.Context, key string, count ...int) (*gvar.Var, error) { var s = []interface{}{key} s = append(s, gconv.Interfaces(count)...) - v, err := r.redis.Do(ctx, "SRandMember", s...) + v, err := r.Operation.Do(ctx, "SRandMember", s...) return v, err } @@ -112,7 +112,7 @@ func (r GroupSet) SRem(ctx context.Context, key string, member interface{}, memb var s = []interface{}{key} s = append(s, member) s = append(s, members...) - v, err := r.redis.Do(ctx, "SRem", s...) + v, err := r.Operation.Do(ctx, "SRem", s...) return v.Int64(), err } @@ -131,7 +131,7 @@ func (r GroupSet) SRem(ctx context.Context, key string, member interface{}, memb // // https://redis.io/commands/smove/ func (r GroupSet) SMove(ctx context.Context, source, destination string, member interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "SMove", source, destination, member) + v, err := r.Operation.Do(ctx, "SMove", source, destination, member) return v.Int64(), err } @@ -141,7 +141,7 @@ func (r GroupSet) SMove(ctx context.Context, source, destination string, member // // https://redis.io/commands/scard/ func (r GroupSet) SCard(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "SCard", key) + v, err := r.Operation.Do(ctx, "SCard", key) return v.Int64(), err } @@ -152,7 +152,7 @@ func (r GroupSet) SCard(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/smembers/ func (r GroupSet) SMembers(ctx context.Context, key string) (gvar.Vars, error) { - v, err := r.redis.Do(ctx, "SMembers", key) + v, err := r.Operation.Do(ctx, "SMembers", key) return v.Vars(), err } @@ -167,7 +167,7 @@ func (r GroupSet) SMembers(ctx context.Context, key string) (gvar.Vars, error) { func (r GroupSet) SMIsMember(ctx context.Context, key, member interface{}, members ...interface{}) ([]int, error) { var s = []interface{}{key, member} s = append(s, members...) - v, err := r.redis.Do(ctx, "SMIsMember", s...) + v, err := r.Operation.Do(ctx, "SMIsMember", s...) return v.Ints(), err } @@ -179,7 +179,7 @@ func (r GroupSet) SMIsMember(ctx context.Context, key, member interface{}, membe func (r GroupSet) SInter(ctx context.Context, key string, keys ...string) (gvar.Vars, error) { var s = []interface{}{key} s = append(s, gconv.Interfaces(keys)...) - v, err := r.redis.Do(ctx, "SInter", s...) + v, err := r.Operation.Do(ctx, "SInter", s...) return v.Vars(), err } @@ -194,7 +194,7 @@ func (r GroupSet) SInter(ctx context.Context, key string, keys ...string) (gvar. func (r GroupSet) SInterStore(ctx context.Context, destination string, key string, keys ...string) (int64, error) { var s = []interface{}{destination, key} s = append(s, gconv.Interfaces(keys)...) - v, err := r.redis.Do(ctx, "SInterStore", s...) + v, err := r.Operation.Do(ctx, "SInterStore", s...) return v.Int64(), err } @@ -206,7 +206,7 @@ func (r GroupSet) SInterStore(ctx context.Context, destination string, key strin func (r GroupSet) SUnion(ctx context.Context, key string, keys ...string) (gvar.Vars, error) { var s = []interface{}{key} s = append(s, gconv.Interfaces(keys)...) - v, err := r.redis.Do(ctx, "SUnion", s...) + v, err := r.Operation.Do(ctx, "SUnion", s...) return v.Vars(), err } @@ -220,7 +220,7 @@ func (r GroupSet) SUnion(ctx context.Context, key string, keys ...string) (gvar. func (r GroupSet) SUnionStore(ctx context.Context, destination, key string, keys ...string) (int64, error) { var s = []interface{}{destination, key} s = append(s, gconv.Interfaces(keys)...) - v, err := r.redis.Do(ctx, "SUnionStore", s...) + v, err := r.Operation.Do(ctx, "SUnionStore", s...) return v.Int64(), err } @@ -233,7 +233,7 @@ func (r GroupSet) SUnionStore(ctx context.Context, destination, key string, keys func (r GroupSet) SDiff(ctx context.Context, key string, keys ...string) (gvar.Vars, error) { var s = []interface{}{key} s = append(s, gconv.Interfaces(keys)...) - v, err := r.redis.Do(ctx, "SDiff", s...) + v, err := r.Operation.Do(ctx, "SDiff", s...) return v.Vars(), err } @@ -247,6 +247,6 @@ func (r GroupSet) SDiff(ctx context.Context, key string, keys ...string) (gvar.V func (r GroupSet) SDiffStore(ctx context.Context, destination string, key string, keys ...string) (int64, error) { var s = []interface{}{destination, key} s = append(s, gconv.Interfaces(keys)...) - v, err := r.redis.Do(ctx, "SDiffStore", s...) + v, err := r.Operation.Do(ctx, "SDiffStore", s...) return v.Int64(), err } diff --git a/contrib/nosql/redis/redis_group_sorted_set.go b/contrib/nosql/redis/redis_group_sorted_set.go index dd9e4b7f15e..667118a231f 100644 --- a/contrib/nosql/redis/redis_group_sorted_set.go +++ b/contrib/nosql/redis/redis_group_sorted_set.go @@ -15,13 +15,13 @@ import ( // GroupSortedSet provides sorted set functions for redis. type GroupSortedSet struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupSortedSet creates and returns GroupSortedSet. func (r *Redis) GroupSortedSet() gredis.IGroupSortedSet { return GroupSortedSet{ - redis: r, + Operation: r.AdapterOperation, } } @@ -55,7 +55,7 @@ func (r GroupSortedSet) ZAdd( for _, item := range members { s = append(s, item.Score, item.Member) } - v, err := r.redis.Do(ctx, "ZAdd", s...) + v, err := r.Operation.Do(ctx, "ZAdd", s...) return v, err } @@ -67,7 +67,7 @@ func (r GroupSortedSet) ZAdd( // // https://redis.io/commands/zscore/ func (r GroupSortedSet) ZScore(ctx context.Context, key string, member interface{}) (float64, error) { - v, err := r.redis.Do(ctx, "ZScore", key, member) + v, err := r.Operation.Do(ctx, "ZScore", key, member) return v.Float64(), err } @@ -84,7 +84,7 @@ func (r GroupSortedSet) ZScore(ctx context.Context, key string, member interface // // https://redis.io/commands/zincrby/ func (r GroupSortedSet) ZIncrBy(ctx context.Context, key string, increment float64, member interface{}) (float64, error) { - v, err := r.redis.Do(ctx, "ZIncrBy", key, increment, member) + v, err := r.Operation.Do(ctx, "ZIncrBy", key, increment, member) return v.Float64(), err } @@ -94,7 +94,7 @@ func (r GroupSortedSet) ZIncrBy(ctx context.Context, key string, increment float // // https://redis.io/commands/zcard/ func (r GroupSortedSet) ZCard(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "ZCard", key) + v, err := r.Operation.Do(ctx, "ZCard", key) return v.Int64(), err } @@ -109,7 +109,7 @@ func (r GroupSortedSet) ZCard(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/zcount/ func (r GroupSortedSet) ZCount(ctx context.Context, key string, min, max string) (int64, error) { - v, err := r.redis.Do(ctx, "ZCount", key, min, max) + v, err := r.Operation.Do(ctx, "ZCount", key, min, max) return v.Int64(), err } @@ -124,7 +124,7 @@ func (r GroupSortedSet) ZRange(ctx context.Context, key string, start, stop int6 if len(option) > 0 { usedOption = option[0] } - v, err := r.redis.Do(ctx, "ZRange", mustMergeOptionToArgs( + v, err := r.Operation.Do(ctx, "ZRange", mustMergeOptionToArgs( []interface{}{key, start, stop}, usedOption, )...) return v.Vars(), err @@ -144,7 +144,7 @@ func (r GroupSortedSet) ZRevRange(ctx context.Context, key string, start, stop i if len(option) > 0 { usedOption = option[0] } - return r.redis.Do(ctx, "ZRevRange", mustMergeOptionToArgs( + return r.Operation.Do(ctx, "ZRevRange", mustMergeOptionToArgs( []interface{}{key, start, stop}, usedOption, )...) } @@ -160,7 +160,7 @@ func (r GroupSortedSet) ZRevRange(ctx context.Context, key string, start, stop i // // https://redis.io/commands/zrank/ func (r GroupSortedSet) ZRank(ctx context.Context, key string, member interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "ZRank", key, member) + v, err := r.Operation.Do(ctx, "ZRank", key, member) return v.Int64(), err } @@ -175,7 +175,7 @@ func (r GroupSortedSet) ZRank(ctx context.Context, key string, member interface{ // // https://redis.io/commands/zrevrank/ func (r GroupSortedSet) ZRevRank(ctx context.Context, key string, member interface{}) (int64, error) { - v, err := r.redis.Do(ctx, "ZRevRank", key, member) + v, err := r.Operation.Do(ctx, "ZRevRank", key, member) return v.Int64(), err } @@ -191,7 +191,7 @@ func (r GroupSortedSet) ZRem(ctx context.Context, key string, member interface{} var s = []interface{}{key} s = append(s, member) s = append(s, members...) - v, err := r.redis.Do(ctx, "ZRem", s...) + v, err := r.Operation.Do(ctx, "ZRem", s...) return v.Int64(), err } @@ -206,7 +206,7 @@ func (r GroupSortedSet) ZRem(ctx context.Context, key string, member interface{} // // https://redis.io/commands/zremrangebyrank/ func (r GroupSortedSet) ZRemRangeByRank(ctx context.Context, key string, start, stop int64) (int64, error) { - v, err := r.redis.Do(ctx, "ZRemRangeByRank", key, start, stop) + v, err := r.Operation.Do(ctx, "ZRemRangeByRank", key, start, stop) return v.Int64(), err } @@ -217,7 +217,7 @@ func (r GroupSortedSet) ZRemRangeByRank(ctx context.Context, key string, start, // // https://redis.io/commands/zremrangebyscore/ func (r GroupSortedSet) ZRemRangeByScore(ctx context.Context, key string, min, max string) (int64, error) { - v, err := r.redis.Do(ctx, "ZRemRangeByScore", key, min, max) + v, err := r.Operation.Do(ctx, "ZRemRangeByScore", key, min, max) return v.Int64(), err } @@ -232,7 +232,7 @@ func (r GroupSortedSet) ZRemRangeByScore(ctx context.Context, key string, min, m // // https://redis.io/commands/zremrangebylex/ func (r GroupSortedSet) ZRemRangeByLex(ctx context.Context, key string, min, max string) (int64, error) { - v, err := r.redis.Do(ctx, "ZRemRangeByLex", key, min, max) + v, err := r.Operation.Do(ctx, "ZRemRangeByLex", key, min, max) return v.Int64(), err } @@ -249,6 +249,6 @@ func (r GroupSortedSet) ZRemRangeByLex(ctx context.Context, key string, min, max // // https://redis.io/commands/zlexcount/ func (r GroupSortedSet) ZLexCount(ctx context.Context, key, min, max string) (int64, error) { - v, err := r.redis.Do(ctx, "ZLexCount", key, min, max) + v, err := r.Operation.Do(ctx, "ZLexCount", key, min, max) return v.Int64(), err } diff --git a/contrib/nosql/redis/redis_group_string.go b/contrib/nosql/redis/redis_group_string.go index 0fd714af368..1cac4ad054e 100644 --- a/contrib/nosql/redis/redis_group_string.go +++ b/contrib/nosql/redis/redis_group_string.go @@ -16,13 +16,13 @@ import ( // GroupString is the function group manager for string operations. type GroupString struct { - redis *Redis + Operation gredis.AdapterOperation } // GroupString is the redis group object for string operations. func (r *Redis) GroupString() gredis.IGroupString { return GroupString{ - redis: r, + Operation: r.AdapterOperation, } } @@ -36,7 +36,7 @@ func (r GroupString) Set(ctx context.Context, key string, value interface{}, opt if len(option) > 0 { usedOption = option[0] } - return r.redis.Do(ctx, "Set", mustMergeOptionToArgs( + return r.Operation.Do(ctx, "Set", mustMergeOptionToArgs( []interface{}{key, value}, usedOption, )...) } @@ -52,7 +52,7 @@ func (r GroupString) Set(ctx context.Context, key string, value interface{}, opt // // https://redis.io/commands/setnx/ func (r GroupString) SetNX(ctx context.Context, key string, value interface{}) (bool, error) { - v, err := r.redis.Do(ctx, "SetNX", key, value) + v, err := r.Operation.Do(ctx, "SetNX", key, value) return v.Bool(), err } @@ -70,7 +70,7 @@ func (r GroupString) SetNX(ctx context.Context, key string, value interface{}) ( // // https://redis.io/commands/setex/ func (r GroupString) SetEX(ctx context.Context, key string, value interface{}, ttlInSeconds int64) error { - _, err := r.redis.Do(ctx, "SetEX", key, ttlInSeconds, value) + _, err := r.Operation.Do(ctx, "SetEX", key, ttlInSeconds, value) return err } @@ -79,7 +79,7 @@ func (r GroupString) SetEX(ctx context.Context, key string, value interface{}, t // // https://redis.io/commands/get/ func (r GroupString) Get(ctx context.Context, key string) (*gvar.Var, error) { - return r.redis.Do(ctx, "Get", key) + return r.Operation.Do(ctx, "Get", key) } // GetDel gets the value of key and delete the key. @@ -88,7 +88,7 @@ func (r GroupString) Get(ctx context.Context, key string) (*gvar.Var, error) { // // https://redis.io/commands/getdel/ func (r GroupString) GetDel(ctx context.Context, key string) (*gvar.Var, error) { - return r.redis.Do(ctx, "GetDel", key) + return r.Operation.Do(ctx, "GetDel", key) } // GetEX is similar to GET, but is a write command with additional options. @@ -99,7 +99,7 @@ func (r GroupString) GetEX(ctx context.Context, key string, option ...gredis.Get if len(option) > 0 { usedOption = option[0] } - return r.redis.Do(ctx, "GetEX", mustMergeOptionToArgs( + return r.Operation.Do(ctx, "GetEX", mustMergeOptionToArgs( []interface{}{key}, usedOption, )...) } @@ -110,7 +110,7 @@ func (r GroupString) GetEX(ctx context.Context, key string, option ...gredis.Get // // https://redis.io/commands/getset/ func (r GroupString) GetSet(ctx context.Context, key string, value interface{}) (*gvar.Var, error) { - return r.redis.Do(ctx, "GetSet", key, value) + return r.Operation.Do(ctx, "GetSet", key, value) } // StrLen returns the length of the string value stored at key. @@ -120,7 +120,7 @@ func (r GroupString) GetSet(ctx context.Context, key string, value interface{}) // // https://redis.io/commands/strlen/ func (r GroupString) StrLen(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "StrLen", key) + v, err := r.Operation.Do(ctx, "StrLen", key) return v.Int64(), err } @@ -130,7 +130,7 @@ func (r GroupString) StrLen(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/append/ func (r GroupString) Append(ctx context.Context, key string, value string) (int64, error) { - v, err := r.redis.Do(ctx, "Append", key, value) + v, err := r.Operation.Do(ctx, "Append", key, value) return v.Int64(), err } @@ -143,7 +143,7 @@ func (r GroupString) Append(ctx context.Context, key string, value string) (int6 // // https://redis.io/commands/setrange/ func (r GroupString) SetRange(ctx context.Context, key string, offset int64, value string) (int64, error) { - v, err := r.redis.Do(ctx, "SetRange", key, offset, value) + v, err := r.Operation.Do(ctx, "SetRange", key, offset, value) return v.Int64(), err } @@ -155,7 +155,7 @@ func (r GroupString) SetRange(ctx context.Context, key string, offset int64, val // // https://redis.io/commands/getrange/ func (r GroupString) GetRange(ctx context.Context, key string, start, end int64) (string, error) { - v, err := r.redis.Do(ctx, "GetRange", key, start, end) + v, err := r.Operation.Do(ctx, "GetRange", key, start, end) return v.String(), err } @@ -166,7 +166,7 @@ func (r GroupString) GetRange(ctx context.Context, key string, start, end int64) // // https://redis.io/commands/incr/ func (r GroupString) Incr(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "Incr", key) + v, err := r.Operation.Do(ctx, "Incr", key) return v.Int64(), err } @@ -178,7 +178,7 @@ func (r GroupString) Incr(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/incrby/ func (r GroupString) IncrBy(ctx context.Context, key string, increment int64) (int64, error) { - v, err := r.redis.Do(ctx, "IncrBy", key, increment) + v, err := r.Operation.Do(ctx, "IncrBy", key, increment) return v.Int64(), err } @@ -186,7 +186,7 @@ func (r GroupString) IncrBy(ctx context.Context, key string, increment int64) (i // // https://redis.io/commands/incrbyfloat/ func (r GroupString) IncrByFloat(ctx context.Context, key string, increment float64) (float64, error) { - v, err := r.redis.Do(ctx, "IncrByFloat", key, increment) + v, err := r.Operation.Do(ctx, "IncrByFloat", key, increment) return v.Float64(), err } @@ -194,7 +194,7 @@ func (r GroupString) IncrByFloat(ctx context.Context, key string, increment floa // // https://redis.io/commands/decr/ func (r GroupString) Decr(ctx context.Context, key string) (int64, error) { - v, err := r.redis.Do(ctx, "Decr", key) + v, err := r.Operation.Do(ctx, "Decr", key) return v.Int64(), err } @@ -202,7 +202,7 @@ func (r GroupString) Decr(ctx context.Context, key string) (int64, error) { // // https://redis.io/commands/decrby/ func (r GroupString) DecrBy(ctx context.Context, key string, decrement int64) (int64, error) { - v, err := r.redis.Do(ctx, "DecrBy", key, decrement) + v, err := r.Operation.Do(ctx, "DecrBy", key, decrement) return v.Int64(), err } @@ -219,7 +219,7 @@ func (r GroupString) MSet(ctx context.Context, keyValueMap map[string]interface{ for k, v := range keyValueMap { args = append(args, k, v) } - _, err := r.redis.Do(ctx, "MSet", args...) + _, err := r.Operation.Do(ctx, "MSet", args...) return err } @@ -233,7 +233,7 @@ func (r GroupString) MSetNX(ctx context.Context, keyValueMap map[string]interfac for k, v := range keyValueMap { args = append(args, k, v) } - v, err := r.redis.Do(ctx, "MSetNX", args...) + v, err := r.Operation.Do(ctx, "MSetNX", args...) return v.Bool(), err } @@ -242,7 +242,7 @@ func (r GroupString) MSetNX(ctx context.Context, keyValueMap map[string]interfac // https://redis.io/commands/mget/ func (r GroupString) MGet(ctx context.Context, keys ...string) (map[string]*gvar.Var, error) { var result = make(map[string]*gvar.Var) - v, err := r.redis.Do(ctx, "MGet", gconv.Interfaces(keys)...) + v, err := r.Operation.Do(ctx, "MGet", gconv.Interfaces(keys)...) if err == nil { values := v.Vars() for i, key := range keys { diff --git a/contrib/nosql/redis/redis_operation.go b/contrib/nosql/redis/redis_operation.go new file mode 100644 index 00000000000..191ca866f6b --- /dev/null +++ b/contrib/nosql/redis/redis_operation.go @@ -0,0 +1,45 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package redis + +import ( + "context" + + "github.com/gogf/gf/v2/container/gvar" + "github.com/gogf/gf/v2/database/gredis" + "github.com/gogf/gf/v2/errors/gerror" +) + +// 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, `Operation 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 +} diff --git a/contrib/rpc/grpcx/grpcx_grpc_server_config.go b/contrib/rpc/grpcx/grpcx_grpc_server_config.go index 47ab655da92..f5e7c878ef1 100644 --- a/contrib/rpc/grpcx/grpcx_grpc_server_config.go +++ b/contrib/rpc/grpcx/grpcx_grpc_server_config.go @@ -8,6 +8,7 @@ package grpcx import ( "context" + "google.golang.org/grpc" "github.com/gogf/gf/v2/frame/g" diff --git a/database/gredis/gredis_adapter.go b/database/gredis/gredis_adapter.go index ad63bdbe913..c941ab0f816 100644 --- a/database/gredis/gredis_adapter.go +++ b/database/gredis/gredis_adapter.go @@ -15,7 +15,24 @@ import ( // Adapter is an interface for universal redis operations. type Adapter interface { AdapterGroup + AdapterOperation +} + +// AdapterGroup is an interface managing group operations for redis. +type AdapterGroup interface { + GroupGeneric() IGroupGeneric + GroupHash() IGroupHash + GroupList() IGroupList + GroupPubSub() IGroupPubSub + GroupScript() IGroupScript + GroupSet() IGroupSet + GroupSortedSet() IGroupSortedSet + GroupString() IGroupString +} +// AdapterOperation is the core operation functions for redis. +// These functions can be easily overwritten by custom implements. +type AdapterOperation interface { // 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. Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) @@ -40,18 +57,6 @@ type Conn interface { Close(ctx context.Context) (err error) } -// AdapterGroup is an interface managing group operations for redis. -type AdapterGroup interface { - GroupGeneric() IGroupGeneric - GroupHash() IGroupHash - GroupList() IGroupList - GroupPubSub() IGroupPubSub - GroupScript() IGroupScript - GroupSet() IGroupSet - GroupSortedSet() IGroupSortedSet - GroupString() IGroupString -} - // ConnCommand is an interface managing some operations bound to certain connection. type ConnCommand interface { // Subscribe subscribes the client to the specified channels. diff --git a/example/httpserver/swagger/main.go b/example/httpserver/swagger/main.go index 2105fcba7be..b494c73d2e3 100644 --- a/example/httpserver/swagger/main.go +++ b/example/httpserver/swagger/main.go @@ -8,17 +8,21 @@ import ( "github.com/gogf/gf/v2/net/ghttp" ) +// HelloReq hello request type HelloReq struct { g.Meta `path:"/hello" method:"get" sort:"1"` Name string `v:"required" dc:"Your name"` } +// HelloRes hello response type HelloRes struct { Reply string `dc:"Reply content"` } +// Hello Controller type Hello struct{} +// Say function func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) { g.Log().Debugf(ctx, `receive say: %+v`, req) res = &HelloRes{ diff --git a/example/httpserver/swagger_set_template/config.yaml b/example/httpserver/swagger_set_template/config.yaml new file mode 100644 index 00000000000..b15f8d00786 --- /dev/null +++ b/example/httpserver/swagger_set_template/config.yaml @@ -0,0 +1,4 @@ +server: + address: ":8199" + openapiPath: "/api.json" + swaggerPath: "/swagger" \ No newline at end of file diff --git a/example/httpserver/swagger_set_template/main.go b/example/httpserver/swagger_set_template/main.go new file mode 100644 index 00000000000..1277de461d4 --- /dev/null +++ b/example/httpserver/swagger_set_template/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "context" + "fmt" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/net/ghttp" +) + +// HelloReq hello request +type HelloReq struct { + g.Meta `path:"/hello" method:"get" sort:"1"` + Name string `v:"required" dc:"Your name"` +} + +// HelloRes hello response +type HelloRes struct { + Reply string `dc:"Reply content"` +} + +// Hello Controller +type Hello struct{} + +// Say function +func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) { + g.Log().Debugf(ctx, `receive say: %+v`, req) + res = &HelloRes{ + Reply: fmt.Sprintf(`Hi %s`, req.Name), + } + return +} + +const ( + MySwaggerUITemplate = ` + + + + + + + SwaggerUI + + + +
+ + + + +` +) + +func main() { + s := g.Server() + s.Use(ghttp.MiddlewareHandlerResponse) + s.Group("/", func(group *ghttp.RouterGroup) { + group.Bind( + new(Hello), + ) + }) + s.SetSwaggerUITemplate(MySwaggerUITemplate) + s.Run() +} diff --git a/example/nosql/redis/adapter/main.go b/example/nosql/redis/adapter/main.go new file mode 100644 index 00000000000..96b7cc827ab --- /dev/null +++ b/example/nosql/redis/adapter/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "context" + "fmt" + + "github.com/gogf/gf/contrib/nosql/redis/v2" + + "github.com/gogf/gf/v2/container/gvar" + "github.com/gogf/gf/v2/database/gredis" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gctx" +) + +var ( + ctx = gctx.New() + group = "cache" + config = gredis.Config{ + Address: "127.0.0.1:6379", + Db: 1, + } +) + +// MyRedis description +type MyRedis struct { + *redis.Redis +} + +// Do implements and overwrites the underlying function Do from Adapter. +func (r *MyRedis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) { + fmt.Println("MyRedis Do:", command, args) + return r.Redis.Do(ctx, command, args...) +} + +func main() { + gredis.RegisterAdapterFunc(func(config *gredis.Config) gredis.Adapter { + r := &MyRedis{redis.New(config)} + r.AdapterOperation = r // This is necessary. + return r + }) + gredis.SetConfig(&config, group) + + _, err := g.Redis(group).Set(ctx, "key", "value") + if err != nil { + g.Log().Fatal(ctx, err) + } + value, err := g.Redis(group).Get(ctx, "key") + if err != nil { + g.Log().Fatal(ctx, err) + } + fmt.Println(value.String()) +} diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 1319aff6b55..4d3355f7bb4 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -222,8 +222,9 @@ type ServerConfig struct { // API & Swagger. // ====================================================================================================== - OpenApiPath string `json:"openapiPath"` // OpenApiPath specifies the OpenApi specification file path. - SwaggerPath string `json:"swaggerPath"` // SwaggerPath specifies the swagger UI path for route registering. + OpenApiPath string `json:"openapiPath"` // OpenApiPath specifies the OpenApi specification file path. + SwaggerPath string `json:"swaggerPath"` // SwaggerPath specifies the swagger UI path for route registering. + SwaggerUITemplate string `json:"swaggerUITemplate"` // SwaggerUITemplate specifies the swagger UI custom template // ====================================================================================================== // Other. diff --git a/net/ghttp/ghttp_server_config_mess.go b/net/ghttp/ghttp_server_config_mess.go index 23d23e8b345..44113877885 100644 --- a/net/ghttp/ghttp_server_config_mess.go +++ b/net/ghttp/ghttp_server_config_mess.go @@ -32,6 +32,11 @@ func (s *Server) SetSwaggerPath(path string) { s.config.SwaggerPath = path } +// SetSwaggerUITemplate sets the Swagger template for server. +func (s *Server) SetSwaggerUITemplate(swaggerUITemplate string) { + s.config.SwaggerUITemplate = swaggerUITemplate +} + // SetOpenApiPath sets the OpenApiPath for server. func (s *Server) SetOpenApiPath(path string) { s.config.OpenApiPath = path diff --git a/net/ghttp/ghttp_server_swagger.go b/net/ghttp/ghttp_server_swagger.go index 2068e89c1f8..7ef7a19181e 100644 --- a/net/ghttp/ghttp_server_swagger.go +++ b/net/ghttp/ghttp_server_swagger.go @@ -7,16 +7,12 @@ package ghttp import ( - "fmt" - "github.com/gogf/gf/v2/text/gstr" ) const ( - swaggerUIDocName = `redoc.standalone.js` - swaggerUIDocNamePlaceHolder = `{SwaggerUIDocName}` - swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}` - swaggerUITemplate = ` + swaggerUIDocURLPlaceHolder = `{SwaggerUIDocUrl}` + swaggerUITemplate = ` @@ -32,7 +28,7 @@ const ( - + ` @@ -44,10 +40,14 @@ func (s *Server) swaggerUI(r *Request) { if s.config.OpenApiPath == "" { return } + var templateContent = swaggerUITemplate + if s.config.SwaggerUITemplate != "" { + templateContent = s.config.SwaggerUITemplate + } + if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir { - content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{ - swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, - swaggerUIDocNamePlaceHolder: gstr.TrimRight(fmt.Sprintf(`//%s%s`, r.Host, r.Server.config.SwaggerPath), "/") + "/" + swaggerUIDocName, + content := gstr.ReplaceByMap(templateContent, map[string]string{ + swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, }) r.Response.Write(content) r.ExitAll() diff --git a/os/gstructs/gstructs_field.go b/os/gstructs/gstructs_field.go index 12213b73d44..639291fd25c 100644 --- a/os/gstructs/gstructs_field.go +++ b/os/gstructs/gstructs_field.go @@ -92,9 +92,26 @@ func (f *Field) OriginalKind() reflect.Kind { reflectType = reflectType.Elem() reflectKind = reflectType.Kind() } + return reflectKind } +// OriginalValue retrieves and returns the original reflect.Value of Field `f`. +func (f *Field) OriginalValue() reflect.Value { + var ( + reflectValue = f.Value + reflectType = reflectValue.Type() + reflectKind = reflectType.Kind() + ) + + for reflectKind == reflect.Ptr && !f.IsNil() { + reflectValue = reflectValue.Elem() + reflectKind = reflectValue.Type().Kind() + } + + return reflectValue +} + // IsEmpty checks and returns whether the value of this Field is empty. func (f *Field) IsEmpty() bool { return empty.IsEmpty(f.Value) diff --git a/os/gtime/gtime_format.go b/os/gtime/gtime_format.go index fd1d214bac9..9214e4b2125 100644 --- a/os/gtime/gtime_format.go +++ b/os/gtime/gtime_format.go @@ -67,6 +67,7 @@ var ( ) // Format formats and returns the formatted result with custom `format`. +// Refer method Layout, if you want to follow stdlib layout. func (t *Time) Format(format string) string { if t == nil { return "" diff --git a/util/gutil/gutil_struct.go b/util/gutil/gutil_struct.go index dee73648119..f0b87cbcd90 100644 --- a/util/gutil/gutil_struct.go +++ b/util/gutil/gutil_struct.go @@ -83,12 +83,20 @@ func FillStructWithDefault(structPtr interface{}) error { } fields, err := gstructs.Fields(gstructs.FieldsInput{ Pointer: reflectValue, - RecursiveOption: gstructs.RecursiveOptionNone, + RecursiveOption: gstructs.RecursiveOptionEmbedded, }) if err != nil { return err } for _, field := range fields { + if field.OriginalKind() == reflect.Struct { + err := FillStructWithDefault(field.OriginalValue().Addr()) + if err != nil { + return err + } + continue + } + if defaultValue := field.TagDefault(); defaultValue != "" { if field.IsEmpty() { field.Value.Set(reflect.ValueOf( @@ -97,5 +105,6 @@ func FillStructWithDefault(structPtr interface{}) error { } } } + return nil } diff --git a/util/gutil/gutil_z_unit_struct_test.go b/util/gutil/gutil_z_unit_struct_test.go index fe4b62e9753..d9ebde8057b 100755 --- a/util/gutil/gutil_z_unit_struct_test.go +++ b/util/gutil/gutil_z_unit_struct_test.go @@ -39,17 +39,44 @@ func Test_StructToSlice(t *testing.T) { func Test_FillStructWithDefault(t *testing.T) { gtest.C(t, func(t *gtest.T) { - type A struct { - V1 int `d:"1.01"` - V2 string `d:"1.01"` - V3 float32 `d:"1.01"` + type myInt int + type Inner1 struct { + I1V1 int + I1V2 bool `d:"true"` } - a := A{} - err := gutil.FillStructWithDefault(&a) + type Inner2 struct { + I2V1 float64 `d:"1.01"` + } + type Inner3 struct { + Inner1 Inner1 + I3V1 myInt `d:"1"` + } + type Inner4 struct { + } + type Outer struct { + O1 int `d:"1.01"` + O2 string `d:"1.01"` + O3 float32 `d:"1.01"` + *Inner1 + O4 bool `d:"true"` + Inner2 + Inner3 Inner3 + Inner4 *Inner4 + } + + outer := Outer{} + err := gutil.FillStructWithDefault(&outer) t.AssertNil(err) - t.Assert(a.V1, `1`) - t.Assert(a.V2, `1.01`) - t.Assert(a.V3, `1.01`) + t.Assert(outer.O1, 1) + t.Assert(outer.O2, `1.01`) + t.Assert(outer.O3, `1.01`) + t.Assert(outer.O4, true) + t.Assert(outer.Inner1, nil) + t.Assert(outer.Inner2.I2V1, `1.01`) + t.Assert(outer.Inner3.I3V1, 1) + t.Assert(outer.Inner3.Inner1.I1V1, 0) + t.Assert(outer.Inner3.Inner1.I1V2, true) + t.Assert(outer.Inner4, nil) }) }