Skip to content

Commit 4cb1119

Browse files
committed
Adding support for the ZLEXCOUNT command on sorted set.
1 parent 71efca9 commit 4cb1119

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/redis.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,30 @@ def zremrangebyrank(key, start, stop)
17011701
end
17021702
end
17031703

1704+
# Count the members, with the same score in a sorted set, within the given lexicographical range.
1705+
#
1706+
# @example Count members matching a
1707+
# redis.zlexcount("zset", "[a", "[a\xff")
1708+
# # => 1
1709+
# @example Count members matching a-z
1710+
# redis.zlexcount("zset", "[a", "[z\xff")
1711+
# # => 26
1712+
#
1713+
# @param [String] key
1714+
# @param [String] min
1715+
# - inclusive minimum is specified by prefixing `(`
1716+
# - exclusive minimum is specified by prefixing `[`
1717+
# @param [String] max
1718+
# - inclusive maximum is specified by prefixing `(`
1719+
# - exclusive maximum is specified by prefixing `[`
1720+
#
1721+
# @return [Fixnum] number of members within the specified lexicographical range
1722+
def zlexcount(key, min, max)
1723+
synchronize do |client|
1724+
client.call([:zlexcount, key, min, max])
1725+
end
1726+
end
1727+
17041728
# Return a range of members with the same score in a sorted set, by lexicographical ordering
17051729
#
17061730
# @example Retrieve members matching a

test/commands_on_sorted_sets_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ class TestCommandsOnSortedSets < Test::Unit::TestCase
88
include Helper::Client
99
include Lint::SortedSets
1010

11+
def test_zlexcount
12+
target_version "2.8.9" do
13+
r.zadd "foo", 0, "aaren"
14+
r.zadd "foo", 0, "abagael"
15+
r.zadd "foo", 0, "abby"
16+
r.zadd "foo", 0, "abbygail"
17+
18+
assert_equal 4, r.zlexcount("foo", "[a", "[a\xff")
19+
assert_equal 4, r.zlexcount("foo", "[aa", "[ab\xff")
20+
assert_equal 3, r.zlexcount("foo", "(aaren", "[ab\xff")
21+
assert_equal 2, r.zlexcount("foo", "[aba", "(abbygail")
22+
assert_equal 1, r.zlexcount("foo", "(aaren", "(abby")
23+
end
24+
end
25+
1126
def test_zrangebylex
1227
target_version "2.8.9" do
1328
r.zadd "foo", 0, "aaren"

0 commit comments

Comments
 (0)