module Oxblood::Commands::Transactions

@see redis.io/topics/transactions

Public Instance Methods

discard() click to toggle source

Discard all commands issued after MULTI @see redis.io/commands/discard

@return [String] 'OK' @return [RError] if called without transaction started

# File lib/oxblood/commands/transactions.rb, line 51
def discard
  run(:DISCARD).tap { connection.transaction_mode = false }
end
exec() click to toggle source

Execute all commands issued after MULTI @see redis.io/commands/exec

@return [Array] each element being the reply to each of the commands

in the atomic transaction

@return [nil] when WATCH was used and execution was aborted

# File lib/oxblood/commands/transactions.rb, line 42
def exec
  run(:EXEC).tap { connection.transaction_mode = false }
end
multi() { || ... } click to toggle source

Mark the start of a transaction block @see redis.io/commands/multi

@example {#exec} will be executed automatically at the end of a block.

session.multi do
  session.ping
  session.ping
end
# => ['PONG', 'PONG']

@example Blockless variant.

session.multi
session.incr(:counter0)
session.incr(:counter1)
session.exec

@return [String] 'OK' @return [RError] if multi called inside transaction

# File lib/oxblood/commands/transactions.rb, line 23
def multi
  response = run(:MULTI).tap do |resp|
    connection.transaction_mode = true if resp == 'OK'
  end

  if block_given?
    yield
    exec
  else
    response
  end
end
unwatch() click to toggle source

Forget about all watched keys @see redis.io/commands/unwatch

@return [String] 'OK'

# File lib/oxblood/commands/transactions.rb, line 74
def unwatch
  run(:UNWATCH)
end
watch(*keys) click to toggle source

Watch the given keys to determine execution of the MULTI/EXEC block @see redis.io/commands/watch

@example

session.set(:cnt, 0)
session.watch(:cnt)
value = session.get(:cnt).to_i
value += 1
session.multi { session.set(:cnt, value) }
# => `['OK']` or `nil` if `cnt` was modified while was watched

@return [String] 'OK'

# File lib/oxblood/commands/transactions.rb, line 66
def watch(*keys)
  run(:WATCH, keys)
end