当前位置:网站首页>利用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,"查询成功");
}
}
效果图
边栏推荐
- [embedded foundation] memory (cache, ram, ROM, flash)
- Réseau neuronal pour la solution détaillée Multi - diagrammes de fondation zéro
- 网络爬虫是什么
- [Yocto RM]8 - OpenEmbedded Kickstart (.wks) Reference
- AI+临床试验患者招募|Massive Bio完成900万美元A轮融资
- 模块化开发
- Original | 2025 to achieve the "five ones" goal! The four products of Jiefang power are officially released
- Want to open an account to buy stock, is it safe to open an account on the Internet?
- What are the requirements for customizing the slip ring for UAV
- Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
猜你喜欢
MapReduce elementary programming practice
【说明】Jmeter乱码的解决方法
Review of drug discovery-03-molecular design and optimization
基于AM335X开发板 ARM Cortex-A8——Acontis EtherCAT主站开发案例
【嵌入式基础】串口通信
What are the requirements for customizing the slip ring for UAV
Lmsoc: a socially sensitive pre training method
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]
Web3 technology initial experience and related learning materials
What is a through-hole conductive slip ring?
随机推荐
Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
LATEX 表格内容居左,中,右
[embedded foundation] memory (cache, ram, ROM, flash)
What are the requirements for customizing the slip ring for UAV
如何高效读书学习
style中的scoped属性和lang属性
How to read a paper
有监督、无监督与半监督学习
[Niuke discussion area] Chapter 4: redis
[DNS resolution] set the name DNSPod resolution for domain name access of COM
Drug interaction prediction based on learning size adaptive molecular substructure
数据人面试指南 | 准备好这几点做到有备无患!
. Mp4 video test address
COSCon'22 讲师征集令
要搞清楚什么是同步,异步,串行,并行,并发,进程,线程,协程
What is digitalization? What is digital transformation? Why do enterprises choose digital transformation?
国外LEAD赚钱的一些习惯
[Yocto RM] 2 - Yocto Project Terms
MySQL 18: execution of write statements
Can you open an account for stock trading in flush? Is it safe?