pub struct Transaction { /* private fields */ }Expand description
A Redis transaction that executes commands atomically
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn new(
connection: Arc<Mutex<dyn TransactionExecutor + Send + Sync>>,
) -> Self
pub fn new( connection: Arc<Mutex<dyn TransactionExecutor + Send + Sync>>, ) -> Self
Create a new transaction
Sourcepub async fn watch(&mut self, keys: Vec<String>) -> RedisResult<()>
pub async fn watch(&mut self, keys: Vec<String>) -> RedisResult<()>
Watch keys for changes before starting the transaction
If any watched key is modified before EXEC, the transaction will be discarded.
§Examples
let mut transaction = client.transaction().await?;
// Watch keys before starting transaction
transaction.watch(vec!["balance".to_string(), "account".to_string()]).await?;
// Add commands
transaction.set("balance", "100");
transaction.incr("account");
// Execute - will fail if watched keys were modified
let results = transaction.exec().await?;Sourcepub async fn unwatch(&mut self) -> RedisResult<()>
pub async fn unwatch(&mut self) -> RedisResult<()>
Unwatch all previously watched keys
Sourcepub fn add_command(&mut self, command: Box<dyn TransactionCommand>) -> &mut Self
pub fn add_command(&mut self, command: Box<dyn TransactionCommand>) -> &mut Self
Add a command to the transaction
Sourcepub fn set(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
pub fn set( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self
Add a SET command to the transaction
Sourcepub fn get(&mut self, key: impl Into<String>) -> &mut Self
pub fn get(&mut self, key: impl Into<String>) -> &mut Self
Add a GET command to the transaction
Sourcepub fn incr(&mut self, key: impl Into<String>) -> &mut Self
pub fn incr(&mut self, key: impl Into<String>) -> &mut Self
Add an INCR command to the transaction
Sourcepub fn decr(&mut self, key: impl Into<String>) -> &mut Self
pub fn decr(&mut self, key: impl Into<String>) -> &mut Self
Add a DECR command to the transaction
Sourcepub fn incr_by(&mut self, key: impl Into<String>, increment: i64) -> &mut Self
pub fn incr_by(&mut self, key: impl Into<String>, increment: i64) -> &mut Self
Add an INCRBY command to the transaction
Sourcepub fn decr_by(&mut self, key: impl Into<String>, decrement: i64) -> &mut Self
pub fn decr_by(&mut self, key: impl Into<String>, decrement: i64) -> &mut Self
Add a DECRBY command to the transaction
Sourcepub fn exists(&mut self, keys: Vec<String>) -> &mut Self
pub fn exists(&mut self, keys: Vec<String>) -> &mut Self
Add an EXISTS command to the transaction
Sourcepub fn expire(&mut self, key: impl Into<String>, seconds: Duration) -> &mut Self
pub fn expire(&mut self, key: impl Into<String>, seconds: Duration) -> &mut Self
Add an EXPIRE command to the transaction
Sourcepub fn ttl(&mut self, key: impl Into<String>) -> &mut Self
pub fn ttl(&mut self, key: impl Into<String>) -> &mut Self
Add a TTL command to the transaction
Sourcepub fn hget(
&mut self,
key: impl Into<String>,
field: impl Into<String>,
) -> &mut Self
pub fn hget( &mut self, key: impl Into<String>, field: impl Into<String>, ) -> &mut Self
Add an HGET command to the transaction
Sourcepub fn hset(
&mut self,
key: impl Into<String>,
field: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
pub fn hset( &mut self, key: impl Into<String>, field: impl Into<String>, value: impl Into<String>, ) -> &mut Self
Add an HSET command to the transaction
Sourcepub async fn exec(&mut self) -> RedisResult<Vec<RespValue>>
pub async fn exec(&mut self) -> RedisResult<Vec<RespValue>>
Execute the transaction
This sends MULTI, queues all commands, and then executes them with EXEC. Returns the results of all commands in the same order they were added.
§Errors
Returns an error if:
- The transaction is empty
- Network communication fails
- Any watched key was modified (returns empty result)
- Any command in the transaction fails
§Examples
let mut transaction = client.transaction().await?;
transaction.set("key1", "value1");
transaction.get("key1");
let results = transaction.exec().await?;
assert_eq!(results.len(), 2);Sourcepub async fn discard(&mut self) -> RedisResult<()>
pub async fn discard(&mut self) -> RedisResult<()>
Discard the transaction
This cancels the transaction and discards all queued commands.
§Examples
let mut transaction = client.transaction().await?;
transaction.set("key1", "value1");
transaction.set("key2", "value2");
// Cancel the transaction
transaction.discard().await?;