Skip to main content

Cache

Struct Cache 

Source
pub struct Cache<T> { /* private fields */ }
Expand description

A policy that caches successful results for a specified duration.

The cache stores the result of the first successful execution and returns it for subsequent calls until the TTL expires.

§Note

This is a simple single-value cache. For more sophisticated caching (keyed cache, LRU eviction, etc.), consider using a dedicated caching library.

§Examples

use do_over::{policy::Policy, cache::Cache, error::DoOverError};
use std::time::Duration;

let cache = Cache::<String>::new(Duration::from_secs(300));

// First call - executes operation
let result: Result<String, DoOverError<String>> = cache.execute(|| async {
    Ok("data".to_string())
}).await;

// Second call - returns cached value
let result: Result<String, DoOverError<String>> = cache.execute(|| async {
    panic!("This won't be called!");
}).await;

Implementations§

Source§

impl<T> Cache<T>
where T: Clone + Send + Sync,

Source

pub fn new(ttl: Duration) -> Self

Create a new cache policy.

§Arguments
  • ttl - Time-to-live for cached values
§Examples
use do_over::cache::Cache;
use std::time::Duration;

// Cache for 5 minutes
let cache = Cache::<String>::new(Duration::from_secs(300));

// Cache for 1 hour
let cache = Cache::<Vec<u8>>::new(Duration::from_secs(3600));
Source

pub async fn invalidate(&self)

Clear the cached value.

The next execution will call the underlying operation.

Source

pub async fn has_cached_value(&self) -> bool

Check if there’s a valid cached value.

Trait Implementations§

Source§

impl<T> Clone for Cache<T>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<T, E> Policy<E> for Cache<T>
where T: Clone + Send + Sync, E: Send + Sync,

Source§

fn execute<'life0, 'async_trait, F, Fut, R>( &'life0 self, f: F, ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>
where F: Fn() -> Fut + Send + Sync + 'async_trait, Fut: Future<Output = Result<R, E>> + Send + 'async_trait, R: Send + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Execute an async operation with this policy’s resilience behavior. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Cache<T>

§

impl<T> !RefUnwindSafe for Cache<T>

§

impl<T> Send for Cache<T>
where T: Send + Sync,

§

impl<T> Sync for Cache<T>
where T: Send + Sync,

§

impl<T> Unpin for Cache<T>

§

impl<T> !UnwindSafe for Cache<T>

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

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

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.