pub struct Cache;Expand description
Cache facade - main entry point for cache operations
Provides static methods for accessing the cache. The cache store is automatically initialized when the server starts.
§Example
ⓘ
use ferro_rs::Cache;
use std::time::Duration;
// Store with TTL
Cache::put("key", &value, Some(Duration::from_secs(3600))).await?;
// Store forever (no expiration)
Cache::forever("key", &value).await?;
// Retrieve
let value: Option<MyType> = Cache::get("key").await?;
// Get or compute (remember pattern)
let value = Cache::remember("key", Some(Duration::from_secs(3600)), || async {
expensive_computation().await
}).await?;Implementations§
Source§impl Cache
impl Cache
Sourcepub fn store() -> Result<Arc<dyn CacheStore>, FrameworkError>
pub fn store() -> Result<Arc<dyn CacheStore>, FrameworkError>
Get the underlying cache store
Sourcepub fn is_initialized() -> bool
pub fn is_initialized() -> bool
Check if the cache is initialized
Sourcepub async fn get<T: DeserializeOwned>(
key: &str,
) -> Result<Option<T>, FrameworkError>
pub async fn get<T: DeserializeOwned>( key: &str, ) -> Result<Option<T>, FrameworkError>
Sourcepub async fn put<T: Serialize>(
key: &str,
value: &T,
ttl: Option<Duration>,
) -> Result<(), FrameworkError>
pub async fn put<T: Serialize>( key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), FrameworkError>
Sourcepub async fn flush() -> Result<(), FrameworkError>
pub async fn flush() -> Result<(), FrameworkError>
Sourcepub async fn remember<T, F, Fut>(
key: &str,
ttl: Option<Duration>,
default: F,
) -> Result<T, FrameworkError>where
T: Serialize + DeserializeOwned,
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, FrameworkError>>,
pub async fn remember<T, F, Fut>(
key: &str,
ttl: Option<Duration>,
default: F,
) -> Result<T, FrameworkError>where
T: Serialize + DeserializeOwned,
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, FrameworkError>>,
Sourcepub async fn remember_forever<T, F, Fut>(
key: &str,
default: F,
) -> Result<T, FrameworkError>where
T: Serialize + DeserializeOwned,
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, FrameworkError>>,
pub async fn remember_forever<T, F, Fut>(
key: &str,
default: F,
) -> Result<T, FrameworkError>where
T: Serialize + DeserializeOwned,
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, FrameworkError>>,
Get an item or store a default value forever
Same as remember but with no expiration.
Auto Trait Implementations§
impl Freeze for Cache
impl RefUnwindSafe for Cache
impl Send for Cache
impl Sync for Cache
impl Unpin for Cache
impl UnsafeUnpin 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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