You can't get time intervals in a single HyperLogLog
key.
Sorted set could be an option;
- You add your users to the sorted set as their entrance date as score and their user id as value with
ZADD
.
- You can use
ZCOUNT
to get total number of unique users in that time interval. I used small numbers for timestamps for example.
127.0.0.1:6379> ZADD activeusers:page:1 1 a1
(integer) 1
127.0.0.1:6379> ZADD activeusers:page:1 1 a2
(integer) 1
127.0.0.1:6379> ZADD activeusers:page:1 3 a5
(integer) 1
127.0.0.1:6379> ZADD activeusers:page:1 116 a7
(integer) 1
127.0.0.1:6379> ZCOUNT activeusers:page:1 60 inf
(integer) 1
127.0.0.1:6379> ZRANGEBYSCORE activeusers:page:1 60 inf
1) "a7"
When you are using ZCOUNT
, you will define MIN
as (current time - (60*60)) and MAX
as inf
, so it will take between (now - 3600 seconds) and (now).
One of the drawbacks for this one is, you need to remove old data from these sets manually via using ZREMRANGEBYSCORE
127.0.0.1:6379> ZREMRANGEBYSCORE activeusers:page:1 -inf 59
(integer) 3
127.0.0.1:6379> ZRANGEBYSCORE activeusers:page:1 -inf inf
1) "a7"
127.0.0.1:6379> ZRANGEBYSCORE activeusers:page:1 -inf inf WITHSCORES
1) "a7"
2) "116"