本篇主要记录和总结工作中用到Redis实现计数统计的一种实现方式
Redis实现计数统计的方式有很多,这里主要简单总结ZSet的一种实现方式。
计数
redisTemplate.opsForZSet().add(BASE_KEY + id, System.currentTimeMillis() + ":" + Math.random(), System.currentTimeMillis());key: BASE_KEY + id
value: System.currentTimeMillis() + “:” + Math.random() 加上Math.random()主要防止并发重复
scope: System.currentTimeMillis())
统计
统计今天scope从0到System.currentTimeMillis()范围的数量
Set<Object> todayClickVolumeSet = redisTemplate.opsForZSet().rangeByScore(BASE_KEY + id(), 0, System.currentTimeMillis());
int todayClickVolume = ObjectU.len(todayClickVolumeSet);