Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change API for BLPOP and friends

  • Loading branch information...
commit 547062d6883d7cc95ec0c69ec2a5eaf2a36a6659 1 parent f972523
@pietern pietern authored
View
7 CHANGELOG.md
@@ -1,5 +1,12 @@
# 3.0 (unreleased)
+* Change API for `BLPOP`, `BRPOP` and `BRPOPLPUSH`. Both `BLPOP` and
+ `BRPOP` now take a single argument equal to a string key, or an array
+ with string keys, followed by an optional hash with a `:timeout` key.
+ `BRPOPLPUSH` also takes an optional hash with a `:timeout` key as last
+ argument for consistency. By default, these commands use a timeout of
+ `0` to not time out.
+
* When `SORT` is passed multiple key patterns to get via the `:get`
option, it now returns an array per result element, holding all `GET`
substitutions.
View
61 lib/redis.rb
@@ -521,25 +521,50 @@ def rpop(key)
# Remove and get the first element in a list, or block until one is available.
#
- # @param [Array<String>] args one or more keys to perform a blocking pop on,
- # followed by a `Fixnum` timeout value
- # @return [nil, Array<String>] tuple of list that was popped from and element
- # that was popped, or nil when the blocking operation timed out
- def blpop(*args)
+ # @example With timeout
+ # list, element = redis.blpop("list", :timeout => 5)
+ # # => nil on timeout
+ # # => ["list", "element"] on success
+ # @example Without timeout
+ # list, element = redis.blpop("list")
+ # # => ["list", "element"]
+ # @example Blocking pop on multiple lists
+ # list, element = redis.blpop(["list", "another_list"])
+ # # => ["list", "element"]
+ #
+ # @param [String, Array<String>] keys one or more keys to perform the
+ # blocking pop on
+ # @param [Hash] options
+ # - `:timeout => Fixnum`: timeout in seconds, defaults to no timeout
+ #
+ # @return [nil, <String, String>]
+ # - `nil` when the operation timed out
+ # - tuple of the list that was popped from and element was popped otherwise
+ def blpop(keys, options = {})
+ timeout = options[:timeout] || 0
+
synchronize do
- @client.call_without_timeout [:blpop, *args]
+ @client.call_without_timeout [:blpop, keys, timeout]
end
end
# Remove and get the last element in a list, or block until one is available.
#
- # @param [Array<String>] args one or more keys to perform a blocking pop on,
- # followed by a `Fixnum` timeout value
- # @return [nil, Array<String>] tuple of list that was popped from and element
- # that was popped, or nil when the blocking operation timed out
- def brpop(*args)
+ # @param [String, Array<String>] keys one or more keys to perform the
+ # blocking pop on
+ # @param [Hash] options
+ # - `:timeout => Fixnum`: timeout in seconds, defaults to no timeout
+ #
+ # @return [nil, <String, String>]
+ # - `nil` when the operation timed out
+ # - tuple of the list that was popped from and element was popped otherwise
+ #
+ # @see #blpop
+ def brpop(keys, options = {})
+ timeout = options[:timeout] || 0
+
synchronize do
- @client.call_without_timeout [:brpop, *args]
+ @client.call_without_timeout [:brpop, keys, timeout]
end
end
@@ -548,9 +573,15 @@ def brpop(*args)
#
# @param [String] source source key
# @param [String] destination destination key
- # @param [Fixnum] timeout
- # @return [nil, String] the element, or nil when the blocking operation timed out
- def brpoplpush(source, destination, timeout)
+ # @param [Hash] options
+ # - `:timeout => Fixnum`: timeout in seconds, defaults to no timeout
+ #
+ # @return [nil, String]
+ # - `nil` when the operation timed out
+ # - the element was popped and pushed otherwise
+ def brpoplpush(source, destination, options = {})
+ timeout = options[:timeout] || 0
+
synchronize do
@client.call_without_timeout [:brpoplpush, source, destination, timeout]
end
View
4 test/commands_on_lists_test.rb
@@ -52,9 +52,9 @@
r.rpush "foo", "s1"
r.rpush "foo", "s2"
- assert_equal "s2", r.brpoplpush("foo", "bar", 1)
+ assert_equal "s2", r.brpoplpush("foo", "bar", :timeout => 1)
- assert_equal nil, r.brpoplpush("baz", "qux", 1)
+ assert_equal nil, r.brpoplpush("baz", "qux", :timeout => 1)
assert_equal ["s2"], r.lrange("bar", 0, -1)
end
View
14 test/distributed_blocking_commands_test.rb
@@ -18,9 +18,9 @@
redis.lpush("foo", "s3")
end
- assert ["foo", "s2"] == r.blpop("foo", 1)
- assert ["foo", "s1"] == r.blpop("foo", 1)
- assert ["foo", "s3"] == r.blpop("foo", 1)
+ assert ["foo", "s2"] == r.blpop("foo", :timeout => 1)
+ assert ["foo", "s1"] == r.blpop("foo", :timeout => 1)
+ assert ["foo", "s3"] == r.blpop("foo", :timeout => 1)
wire.join
end
@@ -35,9 +35,9 @@
redis.rpush("foo", "s3")
end
- assert ["foo", "s2"] == r.brpop("foo", 1)
- assert ["foo", "s1"] == r.brpop("foo", 1)
- assert ["foo", "s3"] == r.brpop("foo", 1)
+ assert ["foo", "s2"] == r.brpop("foo", :timeout => 1)
+ assert ["foo", "s1"] == r.brpop("foo", :timeout => 1)
+ assert ["foo", "s3"] == r.brpop("foo", :timeout => 1)
wire.join
end
@@ -46,7 +46,7 @@
r = Redis::Distributed.new(NODES, :timeout => 1)
assert_nothing_raised do
- r.brpop("foo", 2)
+ r.brpop("foo", :timeout => 2)
end # Errno::EAGAIN raised if socket times out before redis command times out
assert r.nodes.all? { |node| node.client.timeout == 1 }
View
2  test/distributed_commands_on_lists_test.rb
@@ -18,6 +18,6 @@
test "BRPOPLPUSH" do |r|
assert_raise Redis::Distributed::CannotDistribute do
- r.brpoplpush("foo", "bar", 1)
+ r.brpoplpush("foo", "bar", :timeout => 1)
end
end
View
2  test/distributed_commands_requiring_clustering_test.rb
@@ -30,7 +30,7 @@
r.rpush "{qux}foo", "s1"
r.rpush "{qux}foo", "s2"
- assert_equal "s2", r.brpoplpush("{qux}foo", "{qux}bar", 1)
+ assert_equal "s2", r.brpoplpush("{qux}foo", "{qux}bar", :timeout => 1)
assert_equal ["s2"], r.lrange("{qux}bar", 0, -1)
end
Please sign in to comment.
Something went wrong with that request. Please try again.