Attribute Macro cached_proc_macro::io_cached
source · #[io_cached]
Expand description
Define a memoized function using a cache store that implements cached::IOCached
(and
cached::IOCachedAsync
for async functions)
Attributes
map_error
: (string, expr closure) specify a closure used to map any IO-store errors into the error type returned by your function.name
: (optional, string) specify the name for the generated cache, defaults to the function name uppercase.redis
: (optional, bool) default to aRedisCache
orAsyncRedisCache
time
: (optional, u64) specify a cache TTL in seconds, implies the cache type is aTimedCached
orTimedSizedCache
.time_refresh
: (optional, bool) specify whether to refresh the TTL on cache hits.type
: (optional, string type) explicitly specify the cache store type to use.cache_prefix_block
: (optional, string expr) specify an expression used to create the string used as a prefix for all cache keys of this function, e.g.cache_prefix_block = r##"{ "my_prefix" }"##
. When not specified, the cache prefix will be constructed from the name of the function. This could result in unexpected conflicts between io_cached-functions of the same name, so it’s recommended that you specify a prefix you’re sure will be unique.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.type = "TimedCached<u32, 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
ortype
must also be set.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.
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 Type
s or Block
s.