当前位置:网站首页>利用redis bitmap实现人员在线情况监控
利用redis bitmap实现人员在线情况监控
2022-06-27 23:47:00 【OUO~】
一:业务场景
需要显示并统计某些人的在线情况(当天或者指定天数)
二:代码
此处我是利用websocket心跳机制来实现redis bitmap数据的插入
/** * 接收userId */
private String userId = "";
//此处省略-成功建立websocket连接的时候赋值userId
/** * 心跳检测 * * @param message 客户端发送过来的消息 */
@OnMessage
public void onMessage(String message, Session session) throws IOException {
//此处主要设置在线状态-与实例无关代码
redisTemplate.opsForValue().set(Constant.CASE_ONLINE+this.userId
,System.currentTimeMillis(),Constant.ONLINE_MAX_ALIVE_SECOND, TimeUnit.SECONDS);
//当年当月当日--(由于直接用一天的毫秒数计算存在误差所以采用如下笨办法拿到目前步进数)
String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
int m = DateUtil.thisMinute();
int hour = DateUtil.thisHour(true);
//得到步进数--以每天每分钟为单位00:00-24:00--一天最多步进1,440位
long index = hour* 60L +m;
//设立KEY值
String key="ONLINE:"+this.userId+":"+yyyyMMdd;
//存入redis中,index代表当前步进数
redisTemplate.opsForValue().setBit(key, index, true);
//检测是否存在过期时间,若无则设置7天过期
Long expire = redisTemplate.getExpire(key);
if (expire==null || expire < 1L){
redisTemplate.expire(key,7,TimeUnit.DAYS);
}
}
效果如下,注意标记的地方设置为binary才能看到01

下面我们写个查询在线情况的接口
/** * @Author: OUO * @DateTime: 2022/6/1 11:17 * @Description: 获取用户在线情况--当天或者指定某天,格式为YYYYMMDD */
@RequestMapping("/queryOnlineInfo")
public Result queryOnlineInfo(String userId,String times){
Map<String,Object> map=new HashMap<>();
List<String> dateList=new ArrayList<>();
String yyyyMMdd = times;
int m = DateUtil.thisMinute();
int hour = DateUtil.thisHour(true);
//拿到截至到目前时间为止的步进数,--以分钟为单位
long index = hour* 60L +m;
//设置KEY值
String key="ONLINE:"+userId+":"+yyyyMMdd;
//查询自己插入的有效步进总数,例如你成功插入10次,这里结果就为10--此处暂未用上,一般用作统计
Long execute = jsonRedisTemplate.execute((RedisCallback<Long>) conn -> conn.bitCount(key.getBytes()));
List<String> list = new ArrayList<>();
for (int i = 0; i < index; i++) {
//根据步进数 一步步拿到所有的值并赋值
Boolean bit = jsonRedisTemplate.opsForValue().getBit("ONLINE:" + userId + ":" + yyyyMMdd, i);
//建立Y轴数据源
list.add((Boolean.TRUE.equals(bit) ?"在线":"离线"));
//建立X轴数据源
Date dateTime = DateUtil.offsetMinute(DateUtil.beginOfDay(new Date()),i);
String format = DateUtil.format(dateTime, "HH:mm");
dateList.add(format);
}
//为了符合Echarts图所以设置XY
map.put("x",dateList);
map.put("y",list);
return Result.build(true,"200",map,"查询成功");
}
}
效果图

边栏推荐
- Web3 技术初体验以及相关学习资料
- Implementation of timed tasks in laravel framework
- 如何阅读一篇论文
- Chapitre 4: redis
- Can you open an account for stock trading in flush? Is it safe?
- SQL Server 2016 detailed installation tutorial (with registration code and resources)
- Is there any risk in opening an account for flush stock? Is it safe for flush to open an account
- 205. 同构字符串
- 免费、好用、强大的开源笔记软件综合评测
- The contents of the latex table are left, middle and right
猜你喜欢

Web3 technology initial experience and related learning materials
![pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...](/img/b4/8921c3ca5cd5a547768489d4a79202.png)
pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...

万字长文看懂商业智能(BI)|推荐收藏

MapReduce elementary programming practice

机器学习笔记 - 时间序列作为特征

Solon 1.8.3 发布,云原生微服务开发框架

LMSOC:一种对社会敏感的预训练方法
![[description] solution to JMeter garbled code](/img/13/01682d08cbcb47be5d7c21304ef901.png)
[description] solution to JMeter garbled code

【嵌入式基础】串口通信

Cloud assisted privacy collection intersection (server assisted psi) protocol introduction: Learning
随机推荐
数据库的新选择 Amazon Aurora
Arrays.asList()坑
Google Earth engine (GEE) -- an error caused by the imagecollection (error) traversing the image collection
Modular development
Evaluation - rank sum ratio comprehensive evaluation
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
混合app的介绍
How to study efficiently
SPuG - lightweight automatic operation and maintenance platform
Meituan dynamic thread pool practice idea has been open source
自监督学习与药物发现
要搞清楚什么是同步,异步,串行,并行,并发,进程,线程,协程
[description] solution to JMeter garbled code
[Yocto RM] 2 - Yocto Project Terms
Maimai hot post: Why are big factories keen on making wheels?
Qu'est - ce que la numérisation? Qu'est - ce que la transformation numérique? Pourquoi les entreprises choisissent - elles la transformation numérique?
Ten thousand words long article understanding business intelligence (BI) | recommended collection
Transformer论文逐段精读
模块化开发
有监督、无监督与半监督学习