`
01jiangwei01
  • 浏览: 532969 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

redis应用方法

    博客分类:
  • java
阅读更多
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表示没有过期,否则认为已经过期了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics