changeset 104:3d158edc4991

Merge pull request #28 from rsaarelm/fix-shared-cache Fix shared cache
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 04 Aug 2023 16:44:31 +0200
parents d691b841bed6 (current diff) e7d7a568411c (diff)
children 793f2e38a318
files
diffstat 2 files changed, 27 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/shared.rs	Fri Aug 04 16:44:31 2023 +0200
@@ -0,0 +1,16 @@
+use memoize::memoize;
+
+#[memoize(SharedCache)]
+fn hello(arg: String, arg2: usize) -> bool {
+    println!("{} => {}", arg, arg2);
+    arg.len() % 2 == arg2
+}
+
+fn main() {
+    // `hello` is only called once here.
+    assert!(!hello("World".to_string(), 0));
+    assert!(!hello("World".to_string(), 0));
+    // Sometimes one might need the original function.
+    assert!(!memoized_original_hello("World".to_string(), 0));
+    memoized_flush_hello();
+}
--- a/inner/src/lib.rs	Wed Jan 18 22:24:10 2023 +0100
+++ b/inner/src/lib.rs	Fri Aug 04 16:44:31 2023 +0200
@@ -368,9 +368,17 @@
 
     let vis = &func.vis;
 
-    let flusher = quote::quote! {
-        #vis fn #flush_name() {
-            #store_ident.with(|ATTR_MEMOIZE_HM__| ATTR_MEMOIZE_HM__.borrow_mut().clear());
+    let flusher = if options.shared_cache {
+        quote::quote! {
+            #vis fn #flush_name() {
+                #store_ident.lock().unwrap().clear();
+            }
+        }
+    } else {
+        quote::quote! {
+            #vis fn #flush_name() {
+                #store_ident.with(|ATTR_MEMOIZE_HM__| ATTR_MEMOIZE_HM__.borrow_mut().clear());
+            }
         }
     };