Struct bitbazaar::redis::Redis

source ·
pub struct Redis { /* private fields */ }
Available on crate feature redis only.
Expand description

A wrapper around redis to make it more concise to use and not need redis in the downstream Cargo.toml.

This wrapper attempts to return very few errors to help build in automatic redis failure handling into downstream code. All redis errors (availability, unexpected content) will be logged as errors and results returned as None (or similar) where possible.

Implementations§

source§

impl Redis

source

pub fn new<A: Into<String>, B: Into<String>>( redis_conn_str: A, prefix: B ) -> Result<Self, AnyErr>

Create a new global redis wrapper from the given Redis URL (like redis://127.0.0.1).

Note this should only be done once at startup.

source

pub fn conn(&self) -> RedisConn<'_>

Get a RedisConn redis can be called with.

source

pub async fn dlock( &self, lock_id: &str, time_to_live: Duration, wait_up_to: Option<Duration> ) -> Result<RedisLock<'_>, RedisLockErr>

Get a distributed redis lock.

This lock will prevent others getting the lock, until it’s time to live expires. Or the lock is manually released with RedisLock::unlock.

Arguments:

  • lock_id: The resource to lock. Will be used as the key in Redis.
  • ttl: The time to live for this lock. After this time, the lock will be automatically released.
  • wait_up_to: if the lock is busy elsewhere, wait this long trying to get it, before giving up and returning RedisLockErr::Unavailable.
source

pub fn templist( &self, namespace: &'static str, key: impl Into<String>, list_inactive_ttl: Duration, item_inactive_ttl: Duration ) -> RedisTempList

Connect up to a magic redis list that:

  • Has an expiry on the list itself, resetting on each read or write. (each change lives again for expire_after time)
  • Each item in the list has it’s own expiry, so the list is always clean of old items.
  • Each item has a generated unique key, this key can be used to update or delete specific items directly.
  • Returned items are returned newest/last-updated to oldest This makes this distributed data structure perfect for e.g.:
  • recent/temporary logs/events of any sort.
  • pending actions, that can be updated in-place by the creator, but read as part of a list by a viewer etc.
source

pub fn get_inner_pool(&self) -> &Pool

Escape hatch, access the inner deadpool_redis pool.

source

pub fn get_conn_to_each_server(&self) -> Vec<RedisConn<'_>>

Used for dlock, the dlock algo is setup with multiple servers in mind, and synchronising locking between them. It’s a good, future proofed algo, so keeping the multi interface despite the current implementation only using one server.

Trait Implementations§

source§

impl Clone for Redis

source§

fn clone(&self) -> Redis

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Redis

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Redis

§

impl !RefUnwindSafe for Redis

§

impl Send for Redis

§

impl Sync for Redis

§

impl Unpin for Redis

§

impl !UnwindSafe for Redis

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> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

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