Skip to content

Commit

Permalink
Add functionality for flushing the entire memoization cache (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Oct 21, 2022
1 parent 170af06 commit 7e09a19
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ to live:
The cached value will never be older than duration provided and instead
recalculated on the next request.

### Flushing

If you memoize a function `f`, there will be a function called
`memoized_flush_f()` that allows you to clear the memoization cache.

## Contributions

...are always welcome! This being my first procedural-macros crate, I am
Expand Down
13 changes: 9 additions & 4 deletions examples/full_featured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use memoize::memoize;
use std::thread;
use std::time::{Duration, Instant};

#[allow(dead_code)]
#[derive(Debug, Clone)]
struct ComplexStruct {
s: String,
Expand All @@ -25,12 +26,16 @@ fn main() {
{
println!("result: {:?}", hello("ABC".to_string()));
println!("result: {:?}", hello("DEF".to_string()));
println!("result: {:?}", hello("ABC".to_string())); //Same as first
println!("result: {:?}", hello("ABC".to_string())); // Same as first
thread::sleep(core::time::Duration::from_millis(2100));
println!("result: {:?}", hello("ABC".to_string()));
println!("result: {:?}", hello("DEF".to_string()));
println!("result: {:?}", hello("ABC".to_string())); // Same as first
memoized_flush_hello();
println!("result: {:?}", hello("EFG".to_string()));
println!("result: {:?}", hello("ABC".to_string())); //Refreshed
println!("result: {:?}", hello("EFG".to_string())); //Same as first
println!("result: {:?}", hello("ABC".to_string())); //Same as refreshed
println!("result: {:?}", hello("ABC".to_string())); // Refreshed
println!("result: {:?}", hello("EFG".to_string())); // Same as first
println!("result: {:?}", hello("ABC".to_string())); // Same as refreshed
println!("result: {:?}", memoized_original_hello("ABC".to_string()));
}
}
1 change: 1 addition & 0 deletions examples/trivial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fn main() {
assert!(!hello("World".to_string(), 0));
// Sometimes one might need the original function.
assert!(!memoized_original_hello("World".to_string(), 0));
memoized_flush_hello();
}
12 changes: 11 additions & 1 deletion inner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ mod store {
*
* This mechanism can, in principle, be extended (in the source code) to any other cache mechanism.
*
* `memoized_flush_<function name>()` allows you to clear the underlying memoization cache of a
* function. This function is generated with the same visibility as the memoized function.
*
*/
#[proc_macro_attribute]
pub fn memoize(attr: TokenStream, item: TokenStream) -> TokenStream {
Expand All @@ -206,6 +209,7 @@ pub fn memoize(attr: TokenStream, item: TokenStream) -> TokenStream {

let fn_name = &sig.ident.to_string();
let renamed_name = format!("memoized_original_{}", fn_name);
let flush_name = syn::Ident::new(format!("memoized_flush_{}", fn_name).as_str(), sig.span());
let map_name = format!("memoized_mapping_{}", fn_name);

// Extracted from the function signature.
Expand Down Expand Up @@ -313,9 +317,15 @@ pub fn memoize(attr: TokenStream, item: TokenStream) -> TokenStream {

let vis = &func.vis;

let flusher = quote::quote! {
#vis fn #flush_name() {
#store_ident.with(|hm| hm.borrow_mut().clear());
}
};

quote::quote! {
#renamed_fn

#flusher
#store

#[allow(unused_variables)]
Expand Down

0 comments on commit 7e09a19

Please sign in to comment.