Module: Valkey::Commands::TransactionCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/transaction_commands.rb

Overview

This module contains commands related to transactions.

Instance Method Summary collapse

Instance Method Details

#discardString

Discard all commands issued after MULTI.

Returns:

  • (String)

    ‘“OK”`

See Also:



149
150
151
152
153
154
155
156
157
158
# File 'lib/valkey/commands/transaction_commands.rb', line 149

def discard
  send_command(RequestType::DISCARD)
rescue CommandError
  # DISCARD without MULTI is treated similarly to EXEC without MULTI:
  # ignore the server error and return nil.
  nil
ensure
  @in_multi = false
  @queued_commands = []
end

#execnil, Array<...>

Execute all commands issued after MULTI.

Only call this method when ‘#multi` was called without a block.

Returns:

  • (nil, Array<...>)
    • when commands were not executed, ‘nil`

    • when commands were executed, an array with their replies

See Also:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/valkey/commands/transaction_commands.rb', line 115

def exec
  if @in_multi
    begin
      begin
        result = send_command(RequestType::EXEC)
        # If EXEC returns an error object (from array), it's already handled
        result
      rescue CommandError => e
        # If EXEC itself raises an error (like when transaction is aborted),
        # return an array with the error to match expected behavior in tests
        [e]
      end
    ensure
      @in_multi = false
      @queued_commands = []
    end
  else
    # When EXEC is called without a preceding MULTI the server returns an
    # error. The lint tests allow clients to either raise or return nil;
    # we normalize this to simply return nil.
    begin
      send_command(RequestType::EXEC)
    rescue CommandError
      nil
    end
  end
end

#multi {|multi| ... } ⇒ Array<...>

Mark the start of a transaction block.

Examples:

With a block

valkey.multi do |multi|
  multi.set("key", "value")
  multi.incr("counter")
end # => ["OK", 6]

Yields:

  • (multi)

    the commands that are called inside this block are cached and written to the server upon returning from it

Yield Parameters:

Returns:

  • (Array<...>)
    • an array with replies

See Also:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/valkey/commands/transaction_commands.rb', line 27

def multi
  if block_given?
    begin
      @in_multi_block = true
      start_multi
      yield(self)
      exec
    rescue StandardError
      discard
      raise
    ensure
      @in_multi_block = false
    end
  else
    start_multi
    self
  end
end

#unwatchString

Forget about all watched keys.

Returns:

  • (String)

    ‘“OK”`

See Also:



101
102
103
# File 'lib/valkey/commands/transaction_commands.rb', line 101

def unwatch
  send_command(RequestType::UNWATCH)
end

#watch(*keys) ⇒ Object, String

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

Using a block is optional, but is recommended for automatic cleanup.

An ‘#unwatch` is automatically issued if an exception is raised within the block that is a subclass of StandardError and is not a ConnectionError.

Examples:

With a block

valkey.watch("key") do
  if valkey.get("key") == "some value"
    valkey.multi do |multi|
      multi.set("key", "other value")
      multi.incr("counter")
    end
  else
    valkey.unwatch
  end
end
  # => ["OK", 6]

Without a block

valkey.watch("key")
  # => "OK"

Parameters:

  • keys (String, Array<String>)

    one or more keys to watch

Returns:

  • (Object)

    if using a block, returns the return value of the block

  • (String)

    if not using a block, returns ‘“OK”`

See Also:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/valkey/commands/transaction_commands.rb', line 77

def watch(*keys)
  keys.flatten!(1)
  res = send_command(RequestType::WATCH, keys)

  if block_given?
    begin
      yield(self)
    rescue ConnectionError
      raise
    rescue StandardError
      unwatch
      raise
    end
  else
    res
  end
end