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>,
impl<T, F, E> Object<T, F, E>where
F: AsyncFnMut() -> Result<T, E>,
Sourcepub fn new(ttl: Duration, obj: T, refresh_fn: F) -> Object<T, F, E>
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.
Sourcepub async fn new_and_refresh(
ttl: Duration,
refresh_fn: F,
) -> Result<Object<T, F, E>, E>
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.
Sourcepub async fn refresh(&mut self) -> Result<(), E>
pub async fn refresh(&mut self) -> Result<(), E>
Refresh cache immediately and update last update time if refresh success.
Sourcepub fn get(&self) -> Result<&T, TimeoutError>
pub fn get(&self) -> Result<&T, TimeoutError>
Read current cached value or return Error if cache is already expired.
Sourcepub async fn get_or_refresh(&mut self) -> Result<&T, E>
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.
Sourcepub fn time_remain(&self) -> Duration
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.