![]() Use a score of zero for all the elements: > zadd hackers 0 "Alan Kay" 0 "Sophie Wilson" 0 "Richard Stallman" 0 ZREVRANGEBYLEX, ZREMRANGEBYLEX and ZLEXCOUNT.įor example, let's add again our list of famous hackers, but this time The main commands to operate with lexicographical ranges are ZRANGEBYLEX, Redis instance will reply with the same output). Memcmp function, so it is guaranteed that there is no collation, and every Inserted with the same identical score (elements are compared with the C Getting ranges lexicographically, assuming elements in a sorted set are all In version Redis 2.8, a new feature was introduced that allows The ZREVRANK command is also available in order to get the rank, considering Position of an element in the set of the ordered elements. ZREMRANGEBYSCORE is perhaps not the best command name,īut it can be very useful, and returns the number of removed elements.Īnother extremely useful operation defined for sorted set elements The hackers born between 19 from the sorted set: > zremrangebyscore hackers 1940 1960 It's also possible to remove ranges of elements. Infinity and 1950 (both extremes are included). We asked Redis to return all the elements with a score between negative ![]() Use the ZRANGEBYSCORE command to do it: > zrangebyscore hackers -inf 1950 Let's get all the individuals that were born up to 1950 inclusive. It is possible to return scores as well, using the WITHSCORES argument: > zrange hackers 0 -1 withscores Use ZREVRANGE instead of ZRANGE: > zrevrange hackers 0 -1 What if I want to order them the opposite way, youngest to oldest? Here just as it does in the case of the LRANGE command). Note: 0 and -1 means from element index 0 to the last element (-1 works Good, but when we ask for sorted elements Redis does not have to do any work atĪll, it's already all sorted: > zrange hackers 0 -1 Implementation note: Sorted sets are implemented via aĭual-ported data structure containing both a skip list and a hash table, soĮvery time we add an element Redis performs an O(log(N)) operation. With sorted sets it is trivial to return a list of hackers sorted by theirīirth year because actually they are already sorted. Pairs, even if this is not used in the example above. ZADD is also variadic, so you are free to specify multiple score-value (placed before the element to be added) which is the score. > zadd hackers 1940 "Alan Kay"Īs you can see ZADD is similar to SADD, but takes one additional argument Sorted set elements, with their year of birth as "score". Let's start with a simple example, adding a few selected hackers names as B and A strings can't be equal since sorted sets only have unique elements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |