Attribute Macro cached::proc_macro::cached

source ·
#[cached]
Available on crate feature proc_macro only.
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 a SizedCache or TimedSizedCache.
  • time: (optional, u64) specify a cache TTL in seconds, implies the cache type is a TimedCache or TimedSizedCache.
  • 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 to UnboundCache. When unbound is specified, defaults to UnboundCache. When size is specified, defaults to SizedCache. When time is specified, defaults to TimedCached. When size and time are specified, defaults to TimedSizedCache. When ty 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". When key 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) }"##. When convert is specified, key or ty must also be set.
  • result: (optional, bool) If your function returns a Result, only cache Ok values returned by the function.
  • option: (optional, bool) If your function returns an Option, only cache Some values returned by the function.
  • with_cached_flag: (optional, bool) If your function returns a cached::Return or Result<cached::Return, E>, the cached::Return.was_cached flag will be updated when a cached value is returned.
  • result_fallback: (optional, bool) If your function returns a Result and it fails, the cache will instead refresh the recently expired Ok value. In other words, refreshes are best-effort - returning Ok refreshes as usual but Err falls back to the last Ok. 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 implements CloneCached.

§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 Types or Blocks.