Struct Object

Source
pub struct Object<T, F>
where F: AsyncFn() -> Result<T, Box<dyn Error>>,
{ /* private fields */ }
Expand description

Generic cache object which cache an object for given period of time before it return TimeoutError to signal caller to call refresh function before further attempt. The refresh_fn should be async function that return Result of the same type as the cached object. If there’s any error occur inside refresh_fn, it should return Error result back.

Implementations§

Source§

impl<T, F> Object<T, F>
where F: AsyncFn() -> Result<T, Box<dyn Error>>,

Source

pub fn new(ttl: u128, obj: T, refresh_fn: F) -> Object<T, F>

Create a new cached Object with default value specify in second argument. ttl is in milli-second unit. refresh_fn is a function to refresh value and last update time.

Source

pub async fn new_and_refresh( ttl: u128, refresh_fn: F, ) -> Result<Object<T, F>, Box<dyn Error>>

Create a new cached Object and immediately refresh the value instead of using default value. ttl is in milli-second unit. refresh_fn is a function to refresh value and last update time. The different from new function is that it is async and it immediately call refresh_fn.

Source

pub async fn refresh(&mut self) -> Result<(), Box<dyn Error>>

Refresh cache immediately and update last update time if refresh success.

Source

pub fn get(&self) -> Result<&T, TimeoutError>

Read current cached value or return Error if cache is already expired.

Source

pub async fn get_or_refresh(&mut self) -> Result<&T, Box<dyn Error>>

Read current cached value or refresh the value if it is already expired then return the new value.

Trait Implementations§

Source§

impl<T, F> Debug for Object<T, F>
where T: Debug, F: AsyncFn() -> Result<T, Box<dyn Error>>,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for Object<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for Object<T, F>

§

impl<T, F> Send for Object<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for Object<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for Object<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnwindSafe for Object<T, F>
where T: UnwindSafe, F: UnwindSafe,

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