Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction { /* private fields */ }
Expand description

A Redis transaction that executes commands atomically

Implementations§

Source§

impl Transaction

Source

pub fn new( connection: Arc<Mutex<dyn TransactionExecutor + Send + Sync>>, ) -> Self

Create a new transaction

Source

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?;
Source

pub async fn unwatch(&mut self) -> RedisResult<()>

Unwatch all previously watched keys

Source

pub fn add_command(&mut self, command: Box<dyn TransactionCommand>) -> &mut Self

Add a command to the transaction

Source

pub fn set( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self

Add a SET command to the transaction

Source

pub fn get(&mut self, key: impl Into<String>) -> &mut Self

Add a GET command to the transaction

Source

pub fn del(&mut self, keys: Vec<String>) -> &mut Self

Add a DEL command to the transaction

Source

pub fn incr(&mut self, key: impl Into<String>) -> &mut Self

Add an INCR command to the transaction

Source

pub fn decr(&mut self, key: impl Into<String>) -> &mut Self

Add a DECR command to the transaction

Source

pub fn incr_by(&mut self, key: impl Into<String>, increment: i64) -> &mut Self

Add an INCRBY command to the transaction

Source

pub fn decr_by(&mut self, key: impl Into<String>, decrement: i64) -> &mut Self

Add a DECRBY command to the transaction

Source

pub fn exists(&mut self, keys: Vec<String>) -> &mut Self

Add an EXISTS command to the transaction

Source

pub fn expire(&mut self, key: impl Into<String>, seconds: Duration) -> &mut Self

Add an EXPIRE command to the transaction

Source

pub fn ttl(&mut self, key: impl Into<String>) -> &mut Self

Add a TTL command to the transaction

Source

pub fn hget( &mut self, key: impl Into<String>, field: impl Into<String>, ) -> &mut Self

Add an HGET command to the transaction

Source

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

Source

pub fn len(&self) -> usize

Get the number of commands in the transaction

Source

pub fn is_empty(&self) -> bool

Check if the transaction is empty

Source

pub fn clear(&mut self)

Clear all commands from the transaction

Source

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);
Source

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?;

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more