Skip to content

Instantly share code, notes, and snippets.

@justincastilla
Last active April 20, 2022 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justincastilla/7b909fc31dc3fae186e0280213fa5042 to your computer and use it in GitHub Desktop.
Save justincastilla/7b909fc31dc3fae186e0280213fa5042 to your computer and use it in GitHub Desktop.
RU101[LIVE] - Week 2 Streaming Commands

RU101[LIVE] Week 2 Cheat Sheet

Lists

Lets first populate our last_5_played list data type with some songs:

LPUSH last_5_played "Hildur_Gudnadottir:The_Door" "Brian_Eno:Iris"  "Mica_Levi:Love"
"Philip_Glass:Glassworks:_Opening" "Chihei_Hatakeyama:Sad_Ocean" "Colin_Stetson:Reborn"
"Johann_Johannsson:Arrival" "Sarah_Davachi:Gilded" "Christina_Vantzou:Homemade_Mountains"
"Vangelis:Atlas'_push"

Now check the length of the list with the command LLEN

LLEN last_5_played

If we want to reduce the amount of songs in the list to 5, we'll use the LTRIM command. Lets keep the last five songs of the list:

LTRIM last_5_played 5 9 

Note that the start and stop indexes in LTRM are zero-indexed, just like lists.

Sorted Sets

Lets create a sorted set containing online players ranked by their associated scores

ZADD player:leaderboard 1220000 Suzinator 333450 justin_time 95000 KyleOhNo 550000 YourGuyRoyce 165000 SteveDodgeNWeave 22000 BrianTheConquerer 570000 OleksandrTheGreat 115000 Psymon 

To retrieve the Sorted Set, use the ZRANGE command:

ZRANGE player:leaderboard 0 -1

Use the WITHSCORES option to include the individual entries' scores:

ZRANGE player:leaderboard 0 -1 WITHSCORES

Let's find Steve's position in the leaderboard (zrevrank as we want descending score order):

ZREVRANK player:leaderboard SteveDodgeNWeave
(integer) 4

Lets find the top three players with the highest scores with ZREVRANGE and the WITHSCORES option:

ZREVRANGE player:leaderboard 0 2 WITHSCORES

Who is above and below OleksandrTheGreat in the leaderboard? First we find his position...

zrevrank player:leaderboard OleksandrTheGreat
(integer) 1

Then we add one and subtract one to get the position of the players above and below him.

zrevrange player:leaderboard 0 2 withscores
1) "Suzinator"
2) "1220000"
3) "OleksandrTheGreat"
4) "570000"
5) "YourGuyRoyce"
6) "550000"

Sorted Set Intersections

We can apply set theory to sorted sets similar to how we did with the Set datatype. First lets create two sorted sets for two different radio stationsj, KRDS and KSQL:

ZADD station:KRDS:playcount 135 "Hatchie:This_Enchanted" 98 "Gabriels:Blame" 106 "IDLES:The_Beachland_Ballroom" 125 "Snail_Mail:Valentine" 160 "Tres_Leches:Leaving_My_Light_On" 122 "Alt-J:U&ME" 137 "Bonobo:Rosewood" 119 "The_War_on_Drugs:I_Don't_Live_Here_Anymore" 151 "Jarv_Dee_x_Bad_Colours:Clouds"
ZADD station:KSQL:playcount 129 "Hatchie:This_Enchanted" 102 "Gabriels:Blame" 109 "IDLES:The_Beachland_Ballroom" 121 "Snail_Mail:Valentine" 152 "Tres_Leches:Leaving_My_Light_On" 118 "Jarv_Dee_x_Bad_Colours:Clouds" 112 "Curtis_Harding:Can't_Hide_It" 97 "Deep_Sea_Diver:Hand_in_My_Pocket" 138 "Parisalexa:Vroom" 132 "Mitski:Working_for_the_Knife" 124 "Nation_of_Language:This_Fractured_Mind"

Now lets create an intersection between the two sorted sets using ZINTERSTORE. This will create a new sorted set containing only elements that exisdt in both KRDS and KSQL. Note that the scores of both sets will be summed together in this operation.

ZINTERSTORE station:both:top_three 2 station:KRDS:playcount station:KSQL:playcount

We only want to store the highest scoring three songs, so lets trim the length of our intersected sorted set down to three. We'll use the ZREMRANGEBYRANK command. Remember sorted sets by default go from lowest score to highest, so in this case we'll want to remove the first three members (0 throuh 2)

ZREMRANGEBYRANK station:both:top_three 0 2

Now when we inspect the sorted set, we'll see only three members, with the highest scores:

ZREVRANGEBYRANK station:both:top_three 0 -1
1) "Tres_Leches:Leaving_My_Light_On"
2) "312"
3) "Jarv_Dee_x_Bad_Colours:Clouds"
4) "269"
5) "Nation_of_Language:This_Fractured_Mind"
6) "248"

Faceted Search Examples

With faceted searches, we keep sets of event hashes that all share the same attribute. In our course example, we have a set each for all events that have disabled access set to True and all events that have disabled access set to False. We can intersect one of these sets with another to find events that have multiple attributes that we want.

Find all events that have disabled access and are not medal events:

SINTER "fs:medal_event:False" "fs:disabled_access:True"

Find all events that have disabled access, are not medal events, and at the venue Nippon Budokan:

> SINTER "fs:medal_event:False" "fs:disabled_access:True" "fs:venue:Nippon Budokan"
1) "ZCUD-KEPX-EFLA-PTFM"

Inspect that event:

> HGETALL event:"ZCUD-KEPX-EFLA-PTFM"

Big(O) and Performance Considerations

O(1) = constant-time complexity (as efficient as it gets) Example: Redis always maintains a pointer at the left and right of the list, so inserting elements with LPUSH and RPUSH is done in a constant-time complexity.

---> 0 1 2 3 4 5 <---

O(N) = N represents the number of elements traversed within a List, Set, Sorted Set, or Hash

LRANGE list 0 6

Here, we traverse the entire list, so N is 7 in this case

0 1 2 3 4 5 6
^-----------^

O(S + N) = LRANGE example: S represents the offset to start the traversal, N is the amount to traverse

LRANGE list 2 6 Here, we traverse from 2 to 6, so S is 2 and N is 5 in this case

0 1 2 3 4 5 6
    ^-------^

O(N * M) = SINTER example: N is the cardinality of the set you want to first intersect, M is the number of Sets you will be intersecting. Starting with the Set that has the lowest cardinality will ensure a better performance.

With the below sets, if we choose SET A to be the first set to intersect, our cardinality N will be 4. Since we three total sets, our M will be 3 - giving us a performance of (4 * 3) = 12. Notice that if we used SET C first, our cardinality would be 11, and our overall performance would be (3 * 11) = 33

SET A SET B SET C
0 0 1
1 2 3
2 4 5
3 6 7
8 9
10 11
12 13
15
17
19
21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment