cached
cached copied to clipboard
Examples: cache invalidation
I just skimmed through the docs, so bear with me:
I have two functions, one is cached, and another one might invalidate the cache or some key in it. There's no example of how to get ahold of the cache in the second function, in order to be able to call methods like cache_remove or cache_clear.
The second function is not cached, it just invalidates the cache, and there can be an infinite number of functions which need to invalidate a cache.
Other than this missing documentation, looks like an awesome crate. Thank you!
when defining a cached fn
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;
}
}
the actual cache is stored in a global static, in this case COMPUTE, that anyone can acquire a lock on.
Edit: forgot it's in the examples also https://github.com/jaemk/cached/blob/master/examples/basic.rs
Awesome! Is there any isolation going on for this variable, like make it not pub, or make it visible only inside the current module?
I never got around to allowing the caller to specify pub/not-pub for the generated function or the cache itself. As of now, the generated function is pub and then cache is only static. It would probably make sense to update the cache to be pub static to match the fn signature.
https://github.com/jaemk/cached/blob/34e8a2536c338b965fffc95e301ccc666de76c63/src/macros.rs#L19
In the proc macro version, the visibility of the input function is used for the cache and output function.
If I add optional private/public attributes to explicitly specify the visibility of the cache would that address this issue?