cached_proc_macro/lib.rs
1mod cached;
2mod helpers;
3mod io_cached;
4mod once;
5
6use proc_macro::TokenStream;
7
8/// Define a memoized function using a cache store that implements `cached::Cached` (and
9/// `cached::CachedAsync` for async functions)
10///
11/// # Attributes
12/// - `name`: (optional, string) specify the name for the generated cache, defaults to the function name uppercase.
13/// - `size`: (optional, usize) specify an LRU max size, implies the cache type is a `SizedCache` or `TimedSizedCache`.
14/// - `time`: (optional, u64) specify a cache TTL in seconds, implies the cache type is a `TimedCache` or `TimedSizedCache`.
15/// - `time_refresh`: (optional, bool) specify whether to refresh the TTL on cache hits.
16/// - `sync_writes`: (optional, string) specify whether to synchronize the execution and writing of uncached values.
17/// When not specified, the `"default"` behavior will synchronize all keys, locking the whole cache during an
18/// uncached execution. When set to `"by_key"` a per-key lock is used to synchronize uncached execution of
19/// duplicate keys only.
20/// - `ty`: (optional, string type) The cache store type to use. Defaults to `UnboundCache`. When `unbound` is
21/// specified, defaults to `UnboundCache`. When `size` is specified, defaults to `SizedCache`.
22/// When `time` is specified, defaults to `TimedCached`.
23/// When `size` and `time` are specified, defaults to `TimedSizedCache`. When `ty` is
24/// specified, `create` must also be specified.
25/// - `create`: (optional, string expr) specify an expression used to create a new cache store, e.g. `create = r##"{ CacheType::new() }"##`.
26/// - `key`: (optional, string type) specify what type to use for the cache key, e.g. `key = "u32"`.
27/// When `key` is specified, `convert` must also be specified.
28/// - `convert`: (optional, string expr) specify an expression used to convert function arguments to a cache
29/// key, e.g. `convert = r##"{ format!("{}:{}", arg1, arg2) }"##`. When `convert` is specified,
30/// `key` or `ty` must also be set.
31/// - `result`: (optional, bool) If your function returns a `Result`, only cache `Ok` values returned by the function.
32/// - `option`: (optional, bool) If your function returns an `Option`, only cache `Some` values returned by the function.
33/// - `with_cached_flag`: (optional, bool) If your function returns a `cached::Return` or `Result<cached::Return, E>`,
34/// the `cached::Return.was_cached` flag will be updated when a cached value is returned.
35/// - `result_fallback`: (optional, bool) If your function returns a `Result` and it fails, the cache will instead refresh the recently expired `Ok` value.
36/// In other words, refreshes are best-effort - returning `Ok` refreshes as usual but `Err` falls back to the last `Ok`.
37/// This is useful, for example, for keeping the last successful result of a network operation even during network disconnects.
38/// *Note*, this option requires the cache type implements `CloneCached`.
39///
40/// ## Note
41/// The `ty`, `create`, `key`, and `convert` attributes must be in a `String`
42/// This is because darling, which is used for parsing the attributes, does not support directly parsing
43/// attributes into `Type`s or `Block`s.
44#[proc_macro_attribute]
45pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
46 cached::cached(args, input)
47}
48
49/// Define a memoized function using a cache store that implements `cached::Cached` (and
50/// `cached::CachedAsync` for async functions). Function arguments are not used to identify
51/// a cached value, only one value is cached unless a `time` expiry is specified.
52///
53/// # Attributes
54/// - `name`: (optional, string) specify the name for the generated cache, defaults to the function name uppercase.
55/// - `time`: (optional, u64) specify a cache TTL in seconds, implies the cache type is a `TimedCached` or `TimedSizedCache`.
56/// - `sync_writes`: (optional, bool) specify whether to synchronize the execution of writing of uncached values.
57/// - `result`: (optional, bool) If your function returns a `Result`, only cache `Ok` values returned by the function.
58/// - `option`: (optional, bool) If your function returns an `Option`, only cache `Some` values returned by the function.
59/// - `with_cached_flag`: (optional, bool) If your function returns a `cached::Return` or `Result<cached::Return, E>`,
60/// the `cached::Return.was_cached` flag will be updated when a cached value is returned.
61#[proc_macro_attribute]
62pub fn once(args: TokenStream, input: TokenStream) -> TokenStream {
63 once::once(args, input)
64}
65
66/// Define a memoized function using a cache store that implements `cached::IOCached` (and
67/// `cached::IOCachedAsync` for async functions)
68///
69/// # Attributes
70/// - `map_error`: (string, expr closure) specify a closure used to map any IO-store errors into
71/// the error type returned by your function.
72/// - `name`: (optional, string) specify the name for the generated cache, defaults to the function name uppercase.
73/// - `redis`: (optional, bool) default to a `RedisCache` or `AsyncRedisCache`
74/// - `disk`: (optional, bool) use a `DiskCache`, this must be set to true even if `type` and `create` are specified.
75/// - `time`: (optional, u64) specify a cache TTL in seconds, implies the cache type is a `TimedCached` or `TimedSizedCache`.
76/// - `time_refresh`: (optional, bool) specify whether to refresh the TTL on cache hits.
77/// - `ty`: (optional, string type) explicitly specify the cache store type to use.
78/// - `cache_prefix_block`: (optional, string expr) specify an expression used to create the string used as a
79/// prefix for all cache keys of this function, e.g. `cache_prefix_block = r##"{ "my_prefix" }"##`.
80/// When not specified, the cache prefix will be constructed from the name of the function. This
81/// could result in unexpected conflicts between io_cached-functions of the same name, so it's
82/// recommended that you specify a prefix you're sure will be unique.
83/// - `create`: (optional, string expr) specify an expression used to create a new cache store, e.g. `create = r##"{ CacheType::new() }"##`.
84/// - `key`: (optional, string type) specify what type to use for the cache key, e.g. `ty = "TimedCached<u32, u32>"`.
85/// When `key` is specified, `convert` must also be specified.
86/// - `convert`: (optional, string expr) specify an expression used to convert function arguments to a cache
87/// key, e.g. `convert = r##"{ format!("{}:{}", arg1, arg2) }"##`. When `convert` is specified,
88/// `key` or `ty` must also be set.
89/// - `with_cached_flag`: (optional, bool) If your function returns a `cached::Return` or `Result<cached::Return, E>`,
90/// the `cached::Return.was_cached` flag will be updated when a cached value is returned.
91/// - `sync_to_disk_on_cache_change`: (optional, bool) in the case of `DiskCache` specify whether to synchronize the cache to disk each
92/// time the cache changes.
93/// - connection_config: (optional, string expr) specify an expression which returns a `sled::Config`
94/// to give more control over the connection to the disk cache, i.e. useful for controlling the rate at which the cache syncs to disk.
95/// See the docs of `cached::stores::DiskCacheBuilder::connection_config` for more info.
96///
97/// ## Note
98/// The `ty`, `create`, `key`, and `convert` attributes must be in a `String`
99/// This is because darling, which is used for parsing the attributes, does not support directly parsing
100/// attributes into `Type`s or `Block`s.
101#[proc_macro_attribute]
102pub fn io_cached(args: TokenStream, input: TokenStream) -> TokenStream {
103 io_cached::io_cached(args, input)
104}