Module: Familia::Connection::Operations
- Included in:
- Familia::Connection
- Defined in:
- lib/familia/connection/operations.rb
Instance Method Summary collapse
-
#pipelined(&block) {|Redis| ... } ⇒ Array, MultiResult
(also: #pipeline)
Executes Database commands in a pipeline for improved performance.
-
#transaction {|Redis| ... } ⇒ Array, MultiResult
(also: #multi)
Executes Database commands atomically within a transaction (MULTI/EXEC).
-
#with_dbclient {|Redis| ... } ⇒ Object
Provides explicit access to a Database connection.
-
#with_isolated_dbclient(uri = nil) {|Redis| ... } ⇒ Object
Provides explicit access to an isolated Database connection for temporary operations.
Instance Method Details
#pipelined(&block) {|Redis| ... } ⇒ Array, MultiResult Also known as: pipeline
Pipeline vs Transaction Differences:
- Pipeline: Commands executed independently, some may succeed while others fail
- Transaction: All-or-nothing execution, commands are atomic as a group
- Pipeline: Better performance for independent operations
- Transaction: Better consistency for related operations
Connection Handler Compatibility:
- ProviderConnectionHandler: Full pipeline support
- CreateConnectionHandler: Full pipeline support
- FiberTransactionHandler: Blocked (raises OperationModeError)
- FiberConnectionHandler: Blocked (raises OperationModeError)
- DefaultConnectionHandler: Blocked (raises OperationModeError)
Thread Safety: Uses Fiber-local storage to maintain pipeline context across nested calls and ensure proper cleanup even when exceptions occur.
Executes Database commands in a pipeline for improved performance.
Pipelines send multiple commands without waiting for individual responses, reducing network round-trips. Commands execute independently and can succeed or fail without affecting other commands in the pipeline.
Executes Redis commands in a pipeline for improved performance.
Batches multiple Redis commands together and sends them in a single network round-trip, improving performance for multiple independent operations. Returns a MultiResult object containing both success status and command results.
219 220 221 |
# File 'lib/familia/connection/operations.rb', line 219 def pipelined(&block) PipelineCore.execute_pipeline(-> { dbclient }, &block) end |
#transaction {|Redis| ... } ⇒ Array, MultiResult Also known as: multi
Comparison of Database batch operations:
| Feature | Multi/Exec | Pipeline |
|---|---|---|
| Atomicity | Yes | No |
| Performance | Good | Better |
| Error handling | All-or-nothing | Per-command |
| Use case | Data consistency | Bulk operations |
Connection Handler Compatibility:
- FiberTransactionHandler: Supports reentrant transactions
- ProviderConnectionHandler: Full transaction support
- CreateConnectionHandler: Full transaction support
- FiberConnectionHandler: Blocked (raises OperationModeError)
- DefaultConnectionHandler: Blocked (raises OperationModeError)
Thread Safety: Uses Fiber-local storage to maintain transaction context across nested calls and ensure proper cleanup even when exceptions occur.
Executes Database commands atomically within a transaction (MULTI/EXEC).
Database transactions queue commands and execute them atomically as a single unit. All commands succeed together or all fail together, ensuring data consistency.
Executes a Redis transaction (MULTI/EXEC) with proper connection handling.
Provides atomic execution of multiple Redis commands with automatic connection management and operation mode enforcement. Returns a MultiResult object containing both success status and command results.
97 98 99 |
# File 'lib/familia/connection/operations.rb', line 97 def transaction(&) Familia::Connection::TransactionCore.execute_transaction(-> { dbclient }, &) end |
#with_dbclient {|Redis| ... } ⇒ Object
Provides explicit access to a Database connection.
This method is useful when you need direct access to a connection for operations not covered by other methods. The connection is properly managed and returned to the pool (if using connection_provider).
239 240 241 |
# File 'lib/familia/connection/operations.rb', line 239 def with_dbclient(&) yield dbclient end |
#with_isolated_dbclient(uri = nil) {|Redis| ... } ⇒ Object
Provides explicit access to an isolated Database connection for temporary operations.
This method creates a new connection that won't interfere with the cached connection pool, executes the given block with that connection, and ensures the connection is properly closed afterward.
Perfect for database scanning, inspection, or migration operations where you need to access different databases without affecting your models' normal connections.
267 268 269 270 271 272 273 274 |
# File 'lib/familia/connection/operations.rb', line 267 def with_isolated_dbclient(uri = nil, &) client = isolated_dbclient(uri) begin yield client ensure client&.close end end |