From d5512c028c1a97881f65b34c0ba937005d17e9e0 Mon Sep 17 00:00:00 2001 From: yangbing Date: Thu, 7 Jan 2021 17:30:10 +0800 Subject: [PATCH] redis --- adapter/interface.go | 3 ++- adapter/redis.go | 16 ++++++++++++++-- test/mock/adapter/interface.go | 20 ++++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/adapter/interface.go b/adapter/interface.go index 5088ed1..994e863 100644 --- a/adapter/interface.go +++ b/adapter/interface.go @@ -98,6 +98,7 @@ type IRedis interface { MDel(keys []string) bool Exists(key string) bool Incr(key string, delta int) int + IncrBy(key string, delta int) (int, error) Do(cmd string, args ...interface{}) interface{} ExpireAt(key string, timestamp int64) bool Expire(key string, expire time.Duration) bool @@ -113,7 +114,7 @@ type IRedis interface { HGetAll(key string) map[string]*value.Value HMSet(key string, fv ...interface{}) bool HMGet(key string, fields ...interface{}) map[string]*value.Value - HIncrBy(key, field string, delta int) int + HIncrBy(key, field string, delta int) (int,error) ZRange(key string, start, end int) []*value.Value ZRevRange(key string, start, end int) []*value.Value ZRangeWithScores(key string, start, end int) []*redis.ZV diff --git a/adapter/redis.go b/adapter/redis.go index 37dcf0c..cc4edb9 100644 --- a/adapter/redis.go +++ b/adapter/redis.go @@ -205,6 +205,18 @@ func (r *Redis) Incr(key string, delta int) int { return res } +func (r *Redis) IncrBy(key string, delta int) (int, error) { + profile := "Redis.IncrBy" + r.Context().ProfileStart(profile) + defer r.Context().ProfileStop(profile) + defer r.handlePanic() + + res, err := r.client.Incr(key, delta) + r.parseErr(err) + + return res,err +} + // 支持的命令请查阅:Redis.allRedisCmd // args = [0:"key"] // Example: @@ -424,7 +436,7 @@ func (r *Redis) HMGet(key string,fields ...interface{}) map[string]*value.Value return res } -func (r *Redis) HIncrBy(key, field string, delta int) int { +func (r *Redis) HIncrBy(key, field string, delta int) (int,error) { profile := "Redis.HIncrBy" r.Context().ProfileStart(profile) defer r.Context().ProfileStop(profile) @@ -433,7 +445,7 @@ func (r *Redis) HIncrBy(key, field string, delta int) int { res, err := r.client.HIncrBy(key,field, delta) r.parseErr(err) - return res + return res,err } func (r *Redis) ZRange(key string, start, end int) []*value.Value { diff --git a/test/mock/adapter/interface.go b/test/mock/adapter/interface.go index 955be6b..40989c1 100644 --- a/test/mock/adapter/interface.go +++ b/test/mock/adapter/interface.go @@ -1099,6 +1099,21 @@ func (mr *MockIRedisMockRecorder) Incr(key, delta interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Incr", reflect.TypeOf((*MockIRedis)(nil).Incr), key, delta) } +// IncrBy mocks base method. +func (m *MockIRedis) IncrBy(key string, delta int) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IncrBy", key, delta) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// IncrBy indicates an expected call of IncrBy. +func (mr *MockIRedisMockRecorder) IncrBy(key, delta interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IncrBy", reflect.TypeOf((*MockIRedis)(nil).IncrBy), key, delta) +} + // Do mocks base method. func (m *MockIRedis) Do(cmd string, args ...interface{}) interface{} { m.ctrl.T.Helper() @@ -1345,11 +1360,12 @@ func (mr *MockIRedisMockRecorder) HMGet(key interface{}, fields ...interface{}) } // HIncrBy mocks base method. -func (m *MockIRedis) HIncrBy(key, field string, delta int) int { +func (m *MockIRedis) HIncrBy(key, field string, delta int) (int, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "HIncrBy", key, field, delta) ret0, _ := ret[0].(int) - return ret0 + ret1, _ := ret[1].(error) + return ret0, ret1 } // HIncrBy indicates an expected call of HIncrBy.