cached
cached copied to clipboard
Dynamic entry ttl.
I'm building a daemon that needs to cache entries with TTL based on a configuration parameter.
The current approach is as follows:
#[once(time = 2, result = true, sync_writes = true)]
async fn estimate_fees() -> Result
This means that the TTL can only be specified once per function. What would be nice is the ability to specify the TTL dynamically.
Some options:
a. Based on function params: async fn estimate_fees(ttl: Duration, other_params..), that could be configured similar to key generation.
b. Perhaps a memoized method could be defined within a body of another method, which would then pass the ttl param
fn some_method(config: Config) {
define_cache! {
CACHE;
cache_ttl = config.ttl
sync_writes = true
result = true
async fn estimate_fees() -> Result
}
}
Or even a more manual implementation, as long as it provides quick read-only functionality and synchronized writes (to do the init only once per TTL, since this is quite an expensive operation).