jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.57k stars 95 forks source link

How to get all cached values? #148

Closed gitmalong closed 1 year ago

gitmalong commented 1 year ago

It is possible to get all key, value pairs from the cache?


#[cached(
    result = true,
    convert = r##"{ format!("{}", address) }"##,
    type = "SizedCache<String, Option<A>>",
    create = "{ SizedCache::with_size(10000) }",
    sync_writes = true
)]
jaemk commented 1 year ago

Yes, the macro creates a cache instance with the name of the function in all caps (see https://github.com/jaemk/cached/blob/e6814aa94df3b1908b7c35521977f7359867a99a/examples/basic_proc_macro.rs#L37). After you lock the instance, you can use these iterator methods on SizedCached https://github.com/jaemk/cached/blob/e6814aa94df3b1908b7c35521977f7359867a99a/src/stores/sized.rs#L135