Object

Struct Object 

Source
pub struct Object<T, F, E = ()>
where F: AsyncFnMut() -> Result<T, E>,
{ /* 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, E> Object<T, F, E>
where F: AsyncFnMut() -> Result<T, E>,

Source

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

Create a new cached Object with default value specify in second argument. ttl is “time to live” which is duration that the cached value will be return. refresh_fn is a function to refresh value and last update time.

Source

pub async fn new_and_refresh( ttl: Duration, refresh_fn: F, ) -> Result<Object<T, F, E>, E>

Create a new cached Object and immediately refresh the value instead of using default value. ttl is “time to live” which is duration that the cached value will be return. 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<(), E>

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

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

Source

pub fn time_remain(&self) -> Duration

Get time remain that the cache still valid. In other word, time remain before it return TimeoutError on Object::get function.

Trait Implementations§

Source§

impl<T, F, E> CachedObject<T, E> for Object<T, F, E>
where F: AsyncFnMut() -> Result<T, E>,

Source§

fn refresh(&mut self) -> Pin<Box<dyn Future<Output = Result<(), E>> + '_>>

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

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

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

fn get_or_refresh<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<&'a T, E>> + 'a>>
where T: 'a,

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

fn time_remain(&self) -> Duration

Get time remain that the cache still valid. In other word, time remain before it return TimeoutError on Object::get function.
Source§

impl<T: Clone, F, E: Clone> Clone for Object<T, F, E>
where F: AsyncFnMut() -> Result<T, E> + Clone,

Source§

fn clone(&self) -> Object<T, F, E>

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, F, E> Debug for Object<T, F, E>
where T: Debug, F: AsyncFnMut() -> Result<T, E>,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Copy, F, E: Copy> Copy for Object<T, F, E>
where F: AsyncFnMut() -> Result<T, E> + Copy,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, F, E> UnwindSafe for Object<T, F, E>
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> 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.