Skip to content

Commit

Permalink
Merge pull request #166 from brzyangg/fixed-delay-err-master
Browse files Browse the repository at this point in the history
redisext: support TTL
  • Loading branch information
niubell authored Apr 20, 2020
2 parents 794dbb6 + ce5a397 commit 61c3965
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cache/redisext/redisext.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,21 @@ func (m *RedisExt) ZScore(ctx context.Context, key string, member string) (f flo
return
}

func (m *RedisExt) TTL(ctx context.Context, key string) (d time.Duration, err error) {
command := "redisext.TTL"
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 {
d, err = client.TTL(ctx, m.prefixKey(key)).Result()
}
statReqErr(m.namespace, command, err)
return
}
func SetConfiger(ctx context.Context, configerType constants.ConfigerType) error {
fun := "Cache.SetConfiger-->"
configer, err := redis.NewConfiger(configerType)
Expand Down
18 changes: 18 additions & 0 deletions cache/redisext/redisext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,21 @@ func TestRedisExt_HSetNX(t *testing.T) {
assert.Equal(t, false, b)
re.Del(ctx, "hkey1")
}

func TestRedisExt_TTL(t *testing.T) {
ctx := context.Background()
ttl := 10 * time.Second
re := NewRedisExt("base/report", "test")
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)
re.Set(ctx, "getttl3", "test", 0)
d, err = re.TTL(ctx, "getttl3")
assert.NoError(t, err)
assert.Equal(t, -1 * time.Second, d)
re.Del(ctx, "getttl3")
}

0 comments on commit 61c3965

Please sign in to comment.