Skip to content

Commit

Permalink
Merge pull request #170 from brzyangg/feature-redis-sscan
Browse files Browse the repository at this point in the history
add zremrangeby xxx
  • Loading branch information
niubell authored Jun 3, 2020
2 parents 43a19f5 + 4220cb8 commit 80f00d9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
13 changes: 13 additions & 0 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,19 @@ func (m *Client) ZRem(ctx context.Context, key string, members []interface{}) *r
return m.client.ZRem(k, members...)
}

func (m *Client) ZRemRangeByScore(ctx context.Context, key, min, max string) *redis.IntCmd {
k := m.fixKey(key)
m.logSpan(ctx, "ZRemRangeByScore", k)
return m.client.ZRemRangeByScore(k, min, max)
}

func (m *Client) ZRemRangeByRank(ctx context.Context, key string, start int64, stop int64) *redis.IntCmd {
k := m.fixKey(key)
m.logSpan(ctx, "ZRemRangeByRank", k)
return m.client.ZRemRangeByRank(k, start, stop)
}


func (m *Client) ZIncr(ctx context.Context, key string, member redis.Z) *redis.FloatCmd {
k := m.fixKey(key)
m.logSpan(ctx, "ZIncr", k)
Expand Down
32 changes: 32 additions & 0 deletions cache/redisext/redisext.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,38 @@ func (m *RedisExt) ZRem(ctx context.Context, key string, members []interface{})
return
}

func (m *RedisExt) ZRemRangeByScore(ctx context.Context, key, min, max string) (i int64, err error) {
command := "redisext.ZRemRangeByScore"
span, ctx := opentracing.StartSpanFromContext(ctx, command)
st := stime.NewTimeStat()
defer func() {
span.Finish()
statReqDuration(m.namespace, command, st.Millisecond())
}()
client, err := m.getRedisInstance(ctx)
if err == nil {
i, err = client.ZRemRangeByScore(ctx, m.prefixKey(key), min, max).Result()
}
statReqErr(m.namespace, command, err)
return
}

func (m *RedisExt) ZRemRangeByRank(ctx context.Context, key string, start int64, stop int64) (i int64, err error) {
command := "redisext.ZRemRangeByRank"
span, ctx := opentracing.StartSpanFromContext(ctx, command)
st := stime.NewTimeStat()
defer func() {
span.Finish()
statReqDuration(m.namespace, command, st.Millisecond())
}()
client, err := m.getRedisInstance(ctx)
if err == nil {
i, err = client.ZRemRangeByRank(ctx, m.prefixKey(key), start, stop).Result()
}
statReqErr(m.namespace, command, err)
return
}

func (m *RedisExt) ZIncr(ctx context.Context, key string, member Z) (f float64, err error) {
command := "redisext.ZIncr"
span, ctx := opentracing.StartSpanFromContext(ctx, command)
Expand Down
53 changes: 44 additions & 9 deletions cache/redisext/redisext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,17 @@ func TestRedisExt_TTL(t *testing.T) {
ctx := context.Background()
ttl := 10 * time.Second
re := NewRedisExt("base/report", "test")
re.Set(ctx,"getttl1", "test", ttl)
re.Set(ctx, "getttl1", "test", ttl)
d, err := re.TTL(ctx, "getttl1")
assert.NoError(t, err)
assert.Equal(t, ttl, d)
d, err = re.TTL(ctx, "getttl2")
assert.NoError(t, err)
assert.Equal(t, -2 * time.Second, d)
assert.Equal(t, -2*time.Second, d)
re.Set(ctx, "getttl3", "test", 0)
d, err = re.TTL(ctx, "getttl3")
assert.NoError(t, err)
assert.Equal(t, -1 * time.Second, d)
assert.Equal(t, -1*time.Second, d)
re.Del(ctx, "getttl3")
}

Expand All @@ -286,10 +286,10 @@ func TestNewRedisExtNoPrefix(t *testing.T) {
re := NewRedisExtNoPrefix("base/report")
preRedis := NewRedisExt("base/report", "test")

_, err := re.Set(ctx, "set", val, 10 * time.Second)
_, err := re.Set(ctx, "set", val, 10*time.Second)
assert.NoError(t, err)

_, err = preRedis.Set(ctx, "set", val+"prefix", 10 * time.Second)
_, err = preRedis.Set(ctx, "set", val+"prefix", 10*time.Second)
assert.NoError(t, err)

s, err := re.Get(ctx, "set")
Expand All @@ -307,19 +307,19 @@ func TestRedisExt_SScan(t *testing.T) {

i, err := re.SAdd(ctx, "sscantest", 1, 2, 3, 4)
assert.NoError(t, err)
assert.Equal(t, int64(4) , i)
assert.Equal(t, int64(4), i)

b, err := re.SIsMember(ctx, "sscantest", 1)
assert.NoError(t, err)
assert.True(t, b)

i, err = re.SCard(ctx, "sscantest")
assert.NoError(t, err)
assert.Equal(t, int64(4) , i)
assert.Equal(t, int64(4), i)

i ,err = re.SRem(ctx, "sscantest", 1)
i, err = re.SRem(ctx, "sscantest", 1)
assert.NoError(t, err)
assert.Equal(t, int64(1) , i)
assert.Equal(t, int64(1), i)

vals, cursor, err := re.SScan(ctx, "sscantest", 0, "", 4)
assert.NoError(t, err)
Expand All @@ -330,3 +330,38 @@ func TestRedisExt_SScan(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 3, len(s))
}

func TestRedisExt_ZRem(t *testing.T) {
ctx := context.Background()
re := NewRedisExt("base/report", "test")

key := "zremtest"
members := []Z{
{Score: 2000, Member: "jack"},
{Score: 3000, Member: "tom"},
{Score: 5000, Member: "peter"},
}
n, err := re.ZAdd(ctx, key, members)
assert.NoError(t, err)
assert.Equal(t, int64(3), n)

n, err = re.ZRemRangeByRank(ctx, key, 0, 2)
assert.NoError(t, err)
assert.Equal(t, int64(3), n)

n, err = re.ZAdd(ctx, key, members)
assert.NoError(t, err)
assert.Equal(t, int64(3), n)

n, err = re.ZRemRangeByScore(ctx, key, "1500", "3500")
assert.NoError(t, err)
assert.Equal(t, int64(2), n)

ss, err := re.ZRange(ctx, key, 0, -1)
assert.NoError(t, err)
assert.Equal(t, 1, len(ss))
assert.Equal(t, "peter", ss[0])

_, err = re.ZRem(ctx, "zremtest", []interface{}{ss[0]})
assert.NoError(t, err)
}

0 comments on commit 80f00d9

Please sign in to comment.