Zset#
This page focuses on the zset
redis data structure. It discusses all the interesting ways to work with zset
-s.
Is a data structure where the number (score) corresponds to the value. This allows you to select values by the range of their scores.
Examples#
This section focuses on creating examples that we will use to show the possibilities of zset
. It uses Python scripts, but the same result can be achieved using bash.
import redis
import random
from random import randint
from datetime import datetime, timedelta
Users registration time#
Here is a structure that groups users by the time they logged in. We have several users and for each of them we have the time of their registration as a timestamp.
redis_client = redis.Redis()
def random_timestamp(start_date, end_date):
start_timestamp = start_date.timestamp()
end_timestamp = end_date.timestamp()
random_timestamp = random.uniform(start_timestamp, end_timestamp)
return random_timestamp
start_date = datetime(2023, 1, 1)
end_date = datetime(2024, 1, 1)
redis_client.zadd(
"user-reg-time",
{
f"user {i}" : random_timestamp(start_date, end_date)
for i in range(10)
}
)
10
Now let’s print out all the users created and their registration times.
user_reg_time = redis_client.zrange(
"user-reg-time", 0, -1, withscores=True
)
for u, ts in user_reg_time:
print(f"{u.decode('utf-8')} - {datetime.fromtimestamp(ts)}")
user 5 - 2023-01-15 03:11:18.267834
user 7 - 2023-02-08 12:07:52.267084
user 6 - 2023-04-10 12:30:26.944155
user 8 - 2023-04-15 03:47:14.111956
user 0 - 2023-08-14 14:59:04.432249
user 2 - 2023-08-19 12:23:50.033691
user 1 - 2023-08-25 15:50:47.911412
user 4 - 2023-09-30 18:21:52.614111
user 3 - 2023-10-30 16:47:43.209924
user 9 - 2023-12-06 01:44:23.209213
Posts likes#
Here are a bunch of posts created. Each post has a certain number of likes:
redis_client.zadd(
"post-likes",
{
f"post {p}" : randint(50, 1000)
for p in range(10)
}
)
0
Now let’s print out different posts and the number of likes they receive.
post_like = redis_client.zrange(
"post-likes", 0, -1, withscores=True
)
for p, nl in post_like:
print(f"{p.decode('utf-8')} - {nl}")
post 3 - 166.0
post 8 - 404.0
post 4 - 440.0
post 1 - 447.0
post 6 - 479.0
post 9 - 505.0
post 0 - 589.0
post 5 - 880.0
post 2 - 903.0
post 7 - 991.0
Don’t forget to close redis_client
when you are done with the Python code.
redis_client.close()
zrange#
Is a command that allows you to get the elements of the zset according to their ranks.
Let’s say we need to get the 3 earliest registered users. To achieve it we can execute command from the following cell:
!redis-cli ZRANGE user-reg-time 0 2
1) "user 5"
2) "user 7"
3) "user 6"
By adding the withscores
option to the zrange
command, you can specify that you want to see the scores as well. The following example shows what you’ll get:
!redis-cli ZRANGE user-reg-time 0 -1 withscores
1) "user 5"
2) "1673741478.2678339"
3) "user 7"
4) "1675847272.2670836"
5) "user 6"
6) "1681119026.9441555"
7) "user 8"
8) "1681519634.1119561"
9) "user 0"
10) "1692014344.4322493"
11) "user 2"
12) "1692437030.0336912"
13) "user 1"
14) "1692967847.9114122"
15) "user 4"
16) "1696087312.6141107"
17) "user 3"
18) "1698673663.2099235"
19) "user 9"
20) "1701816263.2092133"
zrevrange#
The ZREVRANGE
command in Redis is used to retrieve a range of elements from a sorted set in reverse order based on their scores.
Outputs all users from the most recent registration to the newest.
!redis-cli ZREVRANGE user-reg-time 0 -1
1) "user 9"
2) "user 3"
3) "user 4"
4) "user 1"
5) "user 2"
6) "user 0"
7) "user 8"
8) "user 6"
9) "user 7"
10) "user 5"
zrangebyscore#
You can use the zrangebyscore
command to get values that correspond to a range of scores.
So here is an option to get users who registered before 2023-06-01.
!redis-cli ZRANGEBYSCORE user-reg-time 0 $(date -d "2023-06-01" +%s)
1) "user 5"
2) "user 0"
3) "user 6"
4) "user 7"
5) "user 9"
zscore#
You can access socre by value using the zscore
command.
!redis-cli zscore user-reg-time "user 9"
"1680923027.766844"
zincrby#
This command allows you to increase the score for a given value. The following example show how it works.
So the next cell shows how it works. Here is the number of likes for a particular post, and then zincrby
adds some likes to that post and prints it out again.
%%bash
redis-cli
ZSCORE post-likes "post 1"
ZINCRBY post-likes 20 "post 1"
ZSCORE post-likes "post 1"
447
467
467
The result is that the score has increased by the number of points specified.
Delete examples#
Don’t forget to delete all crated entities to save globar redis clear.
!redis-cli DEL user-reg-time post-likes
(integer) 2