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

边栏推荐
- 混合app的介绍
- pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...
- PV操作原语
- Data analysts too hot? Monthly income 3W? Tell you the true situation of this industry with data
- Why stainless steel swivel
- lefse分析本地实现方法带全部安装文件和所有细节,保证成功。
- [DNS resolution] set the name DNSPod resolution for domain name access of COM
- Interview guide for data person | prepare these points to be prepared!
- 数据库查询优化:主从读写分离及常见问题
- [Yocto RM] 2 - Yocto Project Terms
猜你喜欢

面试官问:能否模拟实现JS的new操作符

TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language

How to build dual channel memory for Lenovo Savior r720

数据库查询优化:主从读写分离及常见问题

Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)

How about the market application strength of large-size conductive slip rings

Review of drug discovery-03-molecular design and optimization

Evaluation - rank sum ratio comprehensive evaluation

一张图弄懂 MIT,BSD,Apache几种开源协议之间的区别

向excel中导入mysql中的数据表
随机推荐
如何理解 Transformer 中的 Query、Key 与 Value
[description] solution to JMeter garbled code
【嵌入式基础】串口通信
什么是数字化?什么是数字化转型?为什么企业选择数字化转型?
Web3 technology initial experience and related learning materials
style中的scoped属性和lang属性
Centos8 operation record command version Yum redis MySQL Nacos JDK
The interviewer asked: this point of JS
Qu'est - ce que la numérisation? Qu'est - ce que la transformation numérique? Pourquoi les entreprises choisissent - elles la transformation numérique?
[Yocto RM]3 - Yocto Project Releases and the Stable Release Process
PV operation primitive
【牛客讨论区】第四章:Redis
I/o limit process and CPU limit process
TI AM3352/54/59 工业核心板硬件说明书
嵌入式必学!硬件资源接口详解——基于ARM AM335X开发板 (下)
解决ionic4 使用hammerjs手势 press 事件,页面无法滚动问题
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
[Niuke discussion area] Chapter 4: redis
Hi, you have a code review strategy to check!
Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)