Say I want to store the 10 most recent additions to a redis sorted set where the 11th addition pushes the 1st off the list. And until it reaches 10 items it just grows normally.
Do I have to check the length on each addition myself and delete the 1st element?
While the same element can't be repeated in a sorted set since every element is unique, it is possible to add multiple different elements having the same score.
In Redis, sorted sets are a data type similar to sets in that both are non repeating groups of strings. The difference is that each member of a sorted set is associated with a score, allowing them to be sorted from the smallest score to the largest.
A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include: Leaderboards.
Redis and PHP Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a set. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score.
I don't think you need to check the length, but have to cap it yourself:
ZREMRANGEBYRANK [KEY] 0 -10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With