Attribute Macro cached::proc_macro::cached
source · [−]#[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 aSizedCacheorTimedSizedCache.time: (optional, u64) specify a cache TTL in seconds, implies the cache type is aTimedCacheorTimedSizedCache.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.type: (optional, string type) The cache store type to use. Defaults toUnboundCache. Whenunboundis specified, defaults toUnboundCache. Whensizeis specified, defaults toSizedCache. Whentimeis specified, defaults toTimedCached. Whensizeandtimeare specified, defaults toTimedSizedCache. Whentypeis specified,createmust 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". Whenkeyis specified,convertmust 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) }"##. Whenconvertis specified,keyortypemust also be set.result: (optional, bool) If your function returns aResult, only cacheOkvalues returned by the function.option: (optional, bool) If your function returns anOption, only cacheSomevalues returned by the function.with_cached_flag: (optional, bool) If your function returns acached::ReturnorResult<cached::Return, E>, thecached::Return.was_cachedflag will be updated when a cached value is returned.
Note
The type, 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 Types or Blocks.