pub struct Redis { /* private fields */ }
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
impl Redis
sourcepub fn new<A: Into<String>, B: Into<String>>(
redis_conn_str: A,
prefix: B
) -> Result<Self, AnyErr>
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.
sourcepub async fn dlock(
&self,
lock_id: &str,
time_to_live: Duration,
wait_up_to: Option<Duration>
) -> Result<RedisLock<'_>, RedisLockErr>
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 returningRedisLockErr::Unavailable
.
sourcepub fn templist(
&self,
namespace: &'static str,
key: impl Into<String>,
list_inactive_ttl: Duration,
item_inactive_ttl: Duration
) -> RedisTempList
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.
sourcepub fn get_inner_pool(&self) -> &Pool
pub fn get_inner_pool(&self) -> &Pool
Escape hatch, access the inner deadpool_redis pool.
sourcepub fn get_conn_to_each_server(&self) -> Vec<RedisConn<'_>>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request