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
-
#discard ⇒ String
Discard all commands issued after MULTI.
-
#exec ⇒ nil, Array<...>
Execute all commands issued after MULTI.
-
#multi {|multi| ... } ⇒ Array<...>
Mark the start of a transaction block.
-
#unwatch ⇒ String
Forget about all watched keys.
-
#watch(*keys) ⇒ Object, String
Watch the given keys to determine execution of the MULTI/EXEC block.
Instance Method Details
#discard ⇒ String
Discard all commands issued after MULTI.
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 |
#exec ⇒ nil, Array<...>
Execute all commands issued after MULTI.
Only call this method when ‘#multi` was called without a block.
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.
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 |
#unwatch ⇒ String
Forget about all watched keys.
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.
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 |