Module: Oxblood::Commands::Transactions

Included in:
Oxblood::Commands
Defined in:
lib/oxblood/commands/transactions.rb

Overview

Instance Method Summary collapse

Instance Method Details

#discardString, RError

Discard all commands issued after MULTI

Returns:

  • (String)

    ‘OK’

  • (RError)

    if called without transaction started

See Also:



51
52
53
# File 'lib/oxblood/commands/transactions.rb', line 51

def discard
  run(:DISCARD).tap { connection.transaction_mode = false }
end

#execArray?

Execute all commands issued after MULTI

Returns:

  • (Array)

    each element being the reply to each of the commands in the atomic transaction

  • (nil)

    when WATCH was used and execution was aborted

See Also:



42
43
44
# File 'lib/oxblood/commands/transactions.rb', line 42

def exec
  run(:EXEC).tap { connection.transaction_mode = false }
end

#multiString, RError

Mark the start of a transaction block

Examples:

#exec will be executed automatically at the end of a block.

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

Blockless variant.

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

Returns:

  • (String)

    ‘OK’

  • (RError)

    if multi called inside transaction

See Also:



23
24
25
26
27
28
29
30
31
32
33
34
# 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

#unwatchString

Forget about all watched keys

Returns:

  • (String)

    ‘OK’

See Also:



74
75
76
# File 'lib/oxblood/commands/transactions.rb', line 74

def unwatch
  run(:UNWATCH)
end

#watch(*keys) ⇒ String

Watch the given keys to determine execution of the MULTI/EXEC block

Examples:

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

Returns:

  • (String)

    ‘OK’

See Also:



66
67
68
# File 'lib/oxblood/commands/transactions.rb', line 66

def watch(*keys)
  run(:WATCH, keys)
end