#[cached]
Expand description
Define a memoized function using a cache store that implements cached::Cached
(and
cached::CachedAsync
for async functions)
§Attributes
name
: (optional, string) specify the name for the generated cache, defaults to the function name uppercase.size
: (optional, usize) specify an LRU max size, implies the cache type is aSizedCache
orTimedSizedCache
.time
: (optional, u64) specify a cache TTL in seconds, implies the cache type is aTimedCache
orTimedSizedCache
.time_refresh
: (optional, bool) specify whether to refresh the TTL on cache hits.sync_writes
: (optional, bool) specify whether to synchronize the execution of writing of uncached values.ty
: (optional, string type) The cache store type to use. Defaults toUnboundCache
. Whenunbound
is specified, defaults toUnboundCache
. Whensize
is specified, defaults toSizedCache
. Whentime
is specified, defaults toTimedCached
. Whensize
andtime
are specified, defaults toTimedSizedCache
. Whenty
is specified,create
must also be specified.create
: (optional, string expr) specify an expression used to create a new cache store, e.g.create = r##"{ CacheType::new() }"##
.key
: (optional, string type) specify what type to use for the cache key, e.g.key = "u32"
. Whenkey
is specified,convert
must also be specified.convert
: (optional, string expr) specify an expression used to convert function arguments to a cache key, e.g.convert = r##"{ format!("{}:{}", arg1, arg2) }"##
. Whenconvert
is specified,key
orty
must also be set.result
: (optional, bool) If your function returns aResult
, only cacheOk
values returned by the function.option
: (optional, bool) If your function returns anOption
, only cacheSome
values returned by the function.with_cached_flag
: (optional, bool) If your function returns acached::Return
orResult<cached::Return, E>
, thecached::Return.was_cached
flag will be updated when a cached value is returned.result_fallback
: (optional, bool) If your function returns aResult
and it fails, the cache will instead refresh the recently expiredOk
value. In other words, refreshes are best-effort - returningOk
refreshes as usual butErr
falls back to the lastOk
. This is useful, for example, for keeping the last successful result of a network operation even during network disconnects. Note, this option requires the cache type implementsCloneCached
.
§Note
The ty
, create
, key
, and convert
attributes must be in a String
This is because darling, which is used for parsing the attributes, does not support directly parsing
attributes into Type
s or Block
s.