Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to set a members limit on a redis sorted set?

Tags:

redis

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?

like image 271
Andrew Arrow Avatar asked Mar 28 '13 22:03

Andrew Arrow


People also ask

Can members be added to a sorted set with the same score?

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.

How does Redis sorted sets work?

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.

What is a Redis sorted set?

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.

What does Redis use to sort the elements of a sorted set?

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.


1 Answers

I don't think you need to check the length, but have to cap it yourself:

ZREMRANGEBYRANK [KEY] 0 -10

like image 128
Ori Dar Avatar answered Sep 20 '22 13:09

Ori Dar