Browse files

Document MULTI and friends

  • Loading branch information...
1 parent b3cb04a commit 46795e7b750174ca98bb83b2e2abac2e3f73f8ce @pietern pietern committed Mar 9, 2012
Showing with 70 additions and 19 deletions.
  1. +70 −19 lib/redis.rb
View
89 lib/redis.rb
@@ -1416,18 +1416,6 @@ def hincrby(key, field, increment)
end
end
- # Discard all commands issued after MULTI.
- #
- # Only call this method when `#multi` was called **without** a block, and
- # `#exec` was not yet called.
- #
- # @return `"OK"`
- def discard
- synchronize do
- @client.call [:discard]
- end
- end
-
# Determine if a hash field exists.
#
# @param [String] key
@@ -1773,27 +1761,60 @@ def pipelined
end
# Watch the given keys to determine execution of the MULTI/EXEC block.
+ #
+ # @param [String, Array<String>] keys one or more keys to watch
+ # @return [String] `OK`
+ #
+ # @see #unwatch
+ # @see #multi
def watch(*keys)
synchronize do
@client.call [:watch, *keys]
end
end
# Forget about all watched keys.
+ #
+ # @return [String] `OK`
+ #
+ # @see #watch
+ # @see #multi
def unwatch
synchronize do
@client.call [:unwatch]
end
end
- # Execute all commands issued after MULTI.
- def exec
- synchronize do
- @client.call [:exec]
- end
- end
-
# Mark the start of a transaction block.
+ #
+ # Passing a block is optional.
+ #
+ # @example With a block
+ # redis.multi do |multi|
+ # multi.set("key", "value")
+ # multi.incr("counter")
+ # end # => ["OK", 6]
+ #
+ # @example Without a block
+ # redis.multi
+ # # => "OK"
+ # redis.set("key", "value")
+ # # => "QUEUED"
+ # redis.incr("counter")
+ # # => "QUEUED"
+ # redis.exec
+ # # => ["OK", 6]
+ #
+ # @yield [multi] the commands that are called inside this block are cached
+ # and written to the server upon returning from it
+ # @yieldparam [Redis] multi `self`
+ #
+ # @return [String, Array<...>]
+ # - when a block is not given, `OK`
+ # - when a block is given, an array with replies
+ #
+ # @see #watch
+ # @see #unwatch
def multi
synchronize do
if !block_given?
@@ -1811,6 +1832,36 @@ def multi
end
end
+ # Execute all commands issued after MULTI.
+ #
+ # Only call this method when `#multi` was called **without** a block.
+ #
+ # @return [nil, Array<...>]
+ # - when commands were not executed, `nil`
+ # - when commands were executed, an array with their replies
+ #
+ # @see #multi
+ # @see #discard
+ def exec
+ synchronize do
+ @client.call [:exec]
+ end
+ end
+
+ # Discard all commands issued after MULTI.
+ #
+ # Only call this method when `#multi` was called **without** a block.
+ #
+ # @return `"OK"`
+ #
+ # @see #multi
+ # @see #exec
+ def discard
+ synchronize do
+ @client.call [:discard]
+ end
+ end
+
# Post a message to a channel.
def publish(channel, message)
synchronize do

0 comments on commit 46795e7

Please sign in to comment.