当前位置:网站首页>利用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,"查询成功");
}
}
效果图

边栏推荐
- TD Hero 线上发布会|7月2日邀你来
- 自监督学习与药物发现
- 一张图弄懂 MIT,BSD,Apache几种开源协议之间的区别
- MySQL十种锁,一篇文章带你全解析
- Evaluation - grey correlation analysis
- 数据库的新选择 Amazon Aurora
- 机器学习笔记 - 时间序列作为特征
- Lmsoc: a socially sensitive pre training method
- Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
- Original | 2025 to achieve the "five ones" goal! The four products of Jiefang power are officially released
猜你喜欢

766. 托普利茨矩阵

药物发现综述-02-分子性质预测

How to build dual channel memory for Lenovo Savior r720

Overview of drug discovery-01 overview of drug discovery

声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
![The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]](/img/56/768f8be9f70bf751f176e40cbb1df2.png)
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]

Implementation of timed tasks in laravel framework

Database query optimization: master-slave read-write separation and common problems

N methods of data De duplication using SQL

MySQL 18: execution of write statements
随机推荐
Lmsoc: a socially sensitive pre training method
深入解析kubernetes controller-runtime
Solve storage problems? WMS warehouse management system solution
Some problems in awk
数据人面试指南 | 准备好这几点做到有备无患!
Is it safe to open an online futures account?
[Yocto RM]1 - System Requirements
【嵌入式基础】内存(Cache,RAM,ROM,Flash)
Scala 基础 (三):运算符和流程控制
[Niuke discussion area] Chapter 4: redis
向excel中导入mysql中的数据表
The research group of Xuyong and duanwenhui of Tsinghua University has developed an efficient and accurate first principles electronic structure deep learning method and program
数据库查询优化:主从读写分离及常见问题
How to optimize the "message" list of IM
同花顺上能炒股开户吗?安全吗?
SQL Server 2016 detailed installation tutorial (with registration code and resources)
零基礎多圖詳解圖神經網絡
完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
N methods of data De duplication using SQL