pub struct Object<T, F>{ /* 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>
impl<T, F> Object<T, F>
Sourcepub fn new(ttl: u128, obj: T, refresh_fn: F) -> Object<T, F>
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.
Sourcepub async fn new_and_refresh(
ttl: u128,
refresh_fn: F,
) -> Result<Object<T, F>, Box<dyn Error>>
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.
Sourcepub async fn refresh(&mut self) -> Result<(), Box<dyn Error>>
pub async fn refresh(&mut self) -> Result<(), Box<dyn Error>>
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.