Caching structures and simplified function memoization
cached provides implementations of several caching structures as well as a handy macro
for defining memoized functions.
Memoized functions defined using #[cached]/#[once]/cached! macros are thread-safe with the backing function-cache wrapped in a mutex/rwlock.
By default, the function-cache is not locked for the duration of the function's execution, so initial (on an empty cache)
concurrent calls of long-running functions with the same arguments will each execute fully and each overwrite
the memoized value as they complete. This mirrors the behavior of Python's functools.lru_cache. To synchronize the execution and caching
of un-cached arguments, specify #[cached(sync_writes = true)] /
#[once(sync_writes = true)].
See cached::stores docs for details about the
cache stores available.
Features
proc_macro: (default) pull in proc macro supportasync: (default) Add CachedAsync trait#[cached], #[once], & cached!Note:
It is recommended you use the two proc-macros (
#[cached],#[once]) as these work with async functions and have more options/features. See theexamples/directory for more sample usage, andcached_proc_macro/src/lib.rsfor the full list of available proc-macro arguments.The declarative macros (
cached!and co.) are still available, but are less maintained and have fewer features.
The basic usage looks like:
```rust use cached::proc_macro::cached;
/// Defines a function named fib that uses a cache implicitly named FIB.
/// By default, the cache will be the function's in all caps.
/// The following line is equivalent to #[cached(name = "FIB", unbound)]
fn fib(n: u64) -> u64 { if n == 0 || n == 1 { return n } fib(n-1) + fib(n-2) } ```
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::cached;
/// Use an lru cache with size 100 and a (String, String) cache key
fn keyed(a: String, b: String) -> usize { let size = a.len() + b.len(); sleep(Duration::new(size as u64, 0)); size } ```
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::cached;
/// Use a timed-lru cache with size 1, a TTL of 60s,
/// and a (usize, usize) cache key
fn keyed(a: usize, b: usize) -> usize { let total = a + b; sleep(Duration::new(total as u64, 0)); total } pub fn main() { keyed(1, 2); // Not cached, will sleep (1+2)s
keyed(1, 2); // Cached, no sleep
sleep(Duration::new(60, 0)); // Sleep for the TTL
keyed(1, 2); // 60s TTL has passed so the cached
// value has expired, will sleep (1+2)s
keyed(1, 2); // Cached, no sleep
keyed(2, 1); // New args, not cached, will sleep (2+1)s
keyed(1, 2); // Was evicted because of lru size of 1,
// will sleep (1+2)s
} ```
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::cached;
/// Use a timed cache with a TTL of 60s
/// that refreshes the entry TTL on cache hit,
/// and a (String, String) cache key
fn keyed(a: String, b: String) -> usize { let size = a.len() + b.len(); sleep(Duration::new(size as u64, 0)); size } ```
```rust use cached::proc_macro::cached;
/// Cache a fallible function. Only Ok results are cached.
fn keyed(a: String) -> Result
```rust use cached::proc_macro::cached;
/// Cache an optional function. Only Some results are cached.
fn keyed(a: String) -> Option
```rust use cached::proc_macro::cached;
/// Cache an optional function. Only Some results are cached.
/// When called concurrently, duplicate argument-calls will be
/// synchronized so as to only run once - the remaining concurrent
/// calls return a cached value.
fn keyed(a: String) -> Option
```rust use cached::proc_macro::cached; use cached::Return;
/// Get a cached::Return value that indicates
/// whether the value returned came from the cache:
/// cached::Return.was_cached.
/// Use an LRU cache and a String cache key.
fn calculate(a: String) -> Return
```rust use cached::proc_macro::cached; use cached::Return;
/// Same as the previous, but returning a Result
fn calculate(a: String) -> Result
```rust use cached::proc_macro::cached; use cached::Return;
/// Same as the previous, but returning an Option
fn calculate(a: String) -> Option
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::cached; use cached::SizedCache;
/// Use an explicit cache-type with a custom creation block and custom cache-key generating block
type = "SizedCache<String, usize>",
create = "{ SizedCache::with_size(100) }",
convert = r#"{ format!("{}{}", a, b) }"#
)] fn keyed(a: &str, b: &str) -> usize { let size = a.len() + b.len(); sleep(Duration::new(size as u64, 0)); size } ```
```rust use cached::proc_macro::once;
/// Only cache the initial function call.
/// Function will be re-executed after the cache
/// expires (according to time seconds).
/// When no (or expired) cache, concurrent calls
/// will synchronize (sync_writes) so the function
/// is only executed once.
fn keyed(a: String) -> Option
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::cached;
/// Use a timed cache with a TTL of 60s. /// Run a background thread to continuously refresh a specific key.
fn keyed(a: &str) -> usize { a.len() } pub fn main() { std::thread::spawn(|| { loop { sleep(Duration::fromsecs(60)); keyedprime_cache("a"); } }); } ```
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::once;
/// Run a background thread to continuously refresh a singleton.
fn keyed() -> String { // do some long http request "some data".tostring() } pub fn main() { std::thread::spawn(|| { loop { sleep(Duration::fromsecs(60)); keyedprimecache(); } }); } ```
```rust use std::thread::sleep; use std::time::Duration; use cached::proc_macro::cached;
/// Run a background thread to continuously refresh every key of a cache
fn keyed(a: &str) -> usize {
a.len()
}
pub fn main() {
std::thread::spawn(|| {
loop {
sleep(Duration::fromsecs(60));
let keys: Vec
#[cached]/cached! defined functions will have their results cached using the function's arguments as a key
(or a specific expression when using cached_key!).
When a cached! defined function is called, the function's cache is first checked for an already
computed (and still valid) value before evaluating the function body.
Due to the requirements of storing arguments and return values in a global cache:
CloneHash + Eq + Clone OR the cached_key!
macro must be used to convert arguments into an owned + Hash + Eq + Clone type.cloned in the process of insertion and retrieval.#[cached]/cached! functions should not be used to produce side-effectual results!#[cached]/cached! functions cannot live directly under impl blocks since cached! expands to a
once_cell initialization and a function definition.#[cached]/cached! functions cannot accept Self types as a parameter.NOTE: Any custom cache that implements cached::Cached can be used with the cached macros in place of the built-ins.
See examples for basic usage of proc-macro &
macro-rules macros and an example of implementing a custom cache-store.
cached! and cached_key! Usage & Options:There are several options depending on how explicit you want to be. See below for a full syntax breakdown.
1.) Using the shorthand will use an unbounded cache.
```rust
/// Defines a function named fib that uses a cache named FIB
cached!{
FIB;
fn fib(n: u64) -> u64 = {
if n == 0 || n == 1 { return n }
fib(n-1) + fib(n-2)
}
}
```
2.) Using the full syntax requires specifying the full cache type and providing
an instance of the cache to use. Note that the cache's key-type is a tuple
of the function argument types. If you would like fine grained control over
the key, you can use the cached_key! macro.
The following example uses a SizedCache (LRU):
```rust
use std::thread::sleep; use std::time::Duration; use cached::SizedCache;
/// Defines a function compute that uses an LRU cache named COMPUTE which has a
/// size limit of 50 items. The cached! macro will implicitly combine
/// the function arguments into a tuple to be used as the cache key.
cached!{
COMPUTE: SizedCache<(u64, u64), u64> = SizedCache::with_size(50);
fn compute(a: u64, b: u64) -> u64 = {
sleep(Duration::new(2, 0));
return a * b;
}
}
```
3.) The cached_key macro functions identically, but allows you to define the
cache key as an expression.
```rust
use std::thread::sleep; use std::time::Duration; use cached::SizedCache;
/// Defines a function named length that uses an LRU cache named LENGTH.
/// The Key = expression is used to explicitly define the value that
/// should be used as the cache key. Here the borrowed arguments are converted
/// to an owned string that can be stored in the global function cache.
cachedkey!{
LENGTH: SizedCache
4.) The cached_result and cached_key_result macros function similarly to cached
and cached_key respectively but the cached function needs to return Result
(or some type alias like io::Result). If the function returns Ok(val) then val
is cached, but errors are not. Note that only the success type needs to implement
Clone, not the error type. When using cached_result and cached_key_result,
the cache type cannot be derived and must always be explicitly specified.
```rust
use cached::UnboundCache;
/// Cache the successes of a function.
/// To use The common macro syntax is: Where: The ```rust use cached::UnboundCache; /// The following usage plugs in expressions to make the macro behave like
/// the }
``` License: MITcached_key_result add a key function as in cached_key.
cached_result!{
MULT: UnboundCache<(u64, u64), u64> = UnboundCache::new(); // Type must always be specified
fn mult(a: u64, b: u64) -> ResultSyntax
rust
cached_key!{
CACHE_NAME: CacheType = CacheInstance;
Key = KeyExpression;
fn func_name(arg1: arg_type, arg2: arg_type) -> return_type = {
// do stuff like normal
return_type
}
}
CACHE_NAME is the unique name used to hold a static ref to the cacheCacheType is the full type of the cacheCacheInstance is any expression that yields an instance of CacheType to be used
as the cache-store, followed by ;cached_key! macro, the "Key" line must be specified. This line must start with
the literal tokens Key =, followed by an expression that evaluates to the key, followed by ;fn func_name(arg1: arg_type) -> return_type is the same form as a regular function signature, with the exception
that functions with no return value must be explicitly stated (e.g. fn func_name(arg: arg_type) -> ())= is the function body assigned to func_name. Note, the function
body can make recursive calls to its cached-self (func_name).Fine grained control using
cached_control!cached_control! macro allows you to provide expressions that get plugged into key areas
of the memoized function. While the cached and cached_result variants are adequate for most
scenarios, it can be useful to have the ability to customize the macro's functionality.[macro_use] extern crate cached;
cached_result! macro.
cached_control!{
CACHE: UnboundCache// Use an owned copy of the argument `input` as the cache key
Key = { input.to_owned() };
// If a cached value exists, it will bind to `cached_val` and
// a `Result` will be returned containing a copy of the cached
// evaluated body. This will return before the function body
// is executed.
PostGet(cached_val) = { return Ok(cached_val.clone()) };
// The result of executing the function body will be bound to
// `body_result`. In this case, the function body returns a `Result`.
// We match on the `Result`, returning an early `Err` if the function errored.
// Otherwise, we pass on the function's result to be cached.
PostExec(body_result) = {
match body_result {
Ok(v) => v,
Err(e) => return Err(e),
}
};
// When inserting the value into the cache we bind
// the to-be-set-value to `set_value` and give back a copy
// of it to be inserted into the cache
Set(set_value) = { set_value.clone() };
// Before returning, print the value that will be returned
Return(return_value) = {
println!("{}", return_value);
Ok(return_value)
};
fn can_fail(input: &str) -> Result<String, String> = {
let len = input.len();
if len < 3 { Ok(format!("{}-{}", input, len)) }
else { Err("too big".to_string()) }
}