ITeye博客

redis应用方法

01jiangwei01 · 2017-11-30 11:47 · java · 企业架构 · Redis · Markdown

1:统计一段时间内,某一时间触发次数

1.1先写值 stringRedisTemplate.opsForZSet().add(key, score, value); stringRedisTemplate.expire(key, 120, TimeUnit.MINUTES); //不要忘记设置有效时长 stringRedisTemplate.removeRange(key, 0, -6); redisTemplate.opsForZSet().removeRangeByScore(keyDispatchFrequencyOrder,min,max)

1.2 做统计 long count = stringRedisTemplate.opsForZSet().count(key,min,max);

1.3 例子:统计2小时内触发规则A的次数

1.3.1 第N次触发规则时 String key = "testA"; long score = System.currentTimeMillis(); stringRedisTemplate.opsForZSet().add(key, score, score); stringRedisTemplate.expire(key, 120, TimeUnit.MINUTES); //不要忘记设置有效时长

long max_socre = System.currentTimeMillis(); long min = max_socre - (2 * 60 * 60 * 1000); long count = stringRedisTemplate.opsForZSet().count(key,min_socre,max_socre);

2:设置缓存 2.1 保存一个值,有效时长为2小时

redisTemplate.opsForValue().set(key, value, 120, TimeUnit.MINUTES); stringRedisTemplate.expire(key, 120, TimeUnit.MINUTES); //补充使用设置时长为2小时

3:删除某个值 3.1

redisTemplate.delete(key); //删除后再get得到的是null

4: 获得某个key的剩余存活时间

4.1 Long ttl = redisTemplate.getExpire(key); 如果大于0表示没有过期,否则认为已经过期了

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。