当前位置:网站首页>使用Redis做某个时间段在线数统计
使用Redis做某个时间段在线数统计
2022-08-04 17:10:00 【Mar丶流年】
前言
一般有范围性的需求都可以使用 sorted set 实现。
sorted set 类型通常用于实现(排行榜,最新的xxx,延迟队列…)
实现
实现很简单,将用户的登录时间转换成时间戳作为权重即可。
假设用户1,3 在20220801号登录
zadd login 1659283200 1
zadd login 1659283200 3
假设用户2,3 在20220802号登录
zadd login 1659369600 2
zadd login 1659369600 3
假设用户4,在20220803号登录
zadd login 1659456000 4
统计20220801至20220802的在线用户数量
# ZCOUNT key min max
# 在线 3
zcount login 1659283200 1659369600
拓展:在上面的基础上统计连续两天都在线的用户及其最后登录时间
假设用户1,3 在20220801号登录
zadd login:20220801 1659283200 1
zadd login:20220801 1659283200 3
假设用户2,3 在20220802号登录
zadd login:20220802 1659369600 2
zadd login:20220802 1659369600 3
假设用户4,在20220803号登录
zadd login:20220803 1659456000 4
统计20220801,20220802 两天都登录的用户及其最后登录时间
# 将两个交集保存至 result:20220801:20220802
# aggregate max 保留最大的权重(即最后登录时间)
# 不使用 aggregate max,权重会累加
zinterstore result:20220801:20220802 2 login:20220801 login:20220802 aggregate max
# 展示数据
zrange result:20220801:20220802 0 -1 withscores
边栏推荐
- 浙江移动咪咕MGV2000-K4_ZJ_S905l2_7661_线刷固件包
- AtCoder Beginner Contest 262 部分题解
- 【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
- 美容院管理系统有哪些促销方式?
- jMeter Transaction Controller 学习笔记
- 【论文阅读】Decision Transformer: Reinforcement Learning via Sequence Modeling
- (1), the sequential storage structure of linear table chain storage structure
- LeetCode 0167. 两数之和 II - 输入有序数组
- 为什么买域名必须实名认证?这样做什么原因?
- 【LeetCode每日一题】——374.猜数字大小
猜你喜欢

九联_UNT400G_S905L2_(联通)_线刷固件包

8月5日,麒麟信安邀您相约鲲鹏开发者创享日·长沙站!

Catering Supply Chain Management System

跨域传递数据(iframe)

What does the product system of a digital financial enterprise look like?

Selenium Webdriver驱动自管理

LeetCode 每日一题——1403. 非递增顺序的最小子序列

How to convert an int attribute into a string in the json format returned by the Go language gin framework?

不需要服务器,教你仅用30行代码搞定实时健康码识别

Nacos集群搭建
随机推荐
WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
机器学习(十八):随机搜索和XGBoost
Unity Apple登录接入
win11如何退出安全模式
谷粒商城笔记
ctfshow 萌新web1-21
Mobile zte ZXV10 B860AV2. 1 - A_S905L2_MT7668_ wire brush the firmware package
越来越火的图数据库到底能做什么?
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
Hubei Mobile HG680-LV_S905L3B_wire brush firmware package
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
【论文阅读】Decision Transformer: Reinforcement Learning via Sequence Modeling
"Distributed cloud best practices" BBS, on August 11, shenzhen
HCIP WPN 实验
Qt自动补全之QCompleter使用
Boost library study notes (1) Installation and configuration
化学制品制造业数智化供应链管理系统:打造智慧供应体系,赋能企业产效提升
Codeforces Round #811 (Div. 3)
【商家联盟】云平台—异业联盟,打造线上线下商业相结合的系统
8月5日,麒麟信安邀您相约鲲鹏开发者创享日·长沙站!