pub struct Cache {
connection_manager: Arc<OnceCell<ConnectionManager>>,
initial_connection_successful: Arc<AtomicBool>,
}
Expand description
Wrapper for accessing a redis cache. Operations are non-blocking and fail gracefully when Redis is unavailable.
Fields§
§connection_manager: Arc<OnceCell<ConnectionManager>>
§initial_connection_successful: Arc<AtomicBool>
Implementations§
Source§impl Cache
impl Cache
Sourcepub fn new(redis_url: &str) -> Result<Cache, UtilError>
pub fn new(redis_url: &str) -> Result<Cache, UtilError>
Creates a new Redis cache instance.
This will succeed even if Redis is unavailable. Cache operations will be no-ops if Redis cannot be connected to. Will retry connecting in the background if initial connection fails.
Sourcepub async fn get_or_set<V, F, Fut, K>(
&self,
key: K,
expires_in: Duration,
f: F,
) -> Result<V, UtilError>
pub async fn get_or_set<V, F, Fut, K>( &self, key: K, expires_in: Duration, f: F, ) -> Result<V, UtilError>
Retrieves a value from cache, or executes the provided function to generate and cache the value.
First checks if the key exists in the cache. If it does, returns the cached value. If not, executes the provided async function, caches its result, and returns it. Operations are non-blocking - if Redis is unavailable, the function is executed immediately.
Sourcepub async fn cache_json<V, K>(
&self,
key: K,
value: &V,
expires_in: Duration,
) -> bool
pub async fn cache_json<V, K>( &self, key: K, value: &V, expires_in: Duration, ) -> bool
Stores the given value in the redis cache as JSON. If Redis is unavailable, this function silently does nothing. This is a non-blocking operation.
Sourcepub async fn get_json<V, K>(&self, key: K) -> Option<V>
pub async fn get_json<V, K>(&self, key: K) -> Option<V>
Retrieves and deserializes a value from cache. If Redis is unavailable, returns None. This is a non-blocking operation.
Sourcepub async fn invalidate<K>(&self, key: K) -> bool
pub async fn invalidate<K>(&self, key: K) -> bool
Delete a key from the cache. Returns true if the key was deleted, false otherwise. This is a non-blocking operation.
Sourcepub fn initial_connection_successful(&self) -> bool
pub fn initial_connection_successful(&self) -> bool
Returns whether the initial connection was successful
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Cache
impl !RefUnwindSafe for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl !UnwindSafe for Cache
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more