Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created June 29, 2021 21:02
Show Gist options
  • Save MasterDuke17/c2769796f3a693ee5d836c5a5f73788e to your computer and use it in GitHub Desktop.
Save MasterDuke17/c2769796f3a693ee5d836c5a5f73788e to your computer and use it in GitHub Desktop.
diff --git a/lib/experimental.rakumod b/lib/experimental.rakumod
index 4f6694e5b..c441ad4ec 100644
--- a/lib/experimental.rakumod
+++ b/lib/experimental.rakumod
@@ -3,11 +3,16 @@
package EXPORT::cached {
multi sub trait_mod:<is>(Routine $r, :$cached!) {
my %cache;
+ my $lock = Lock.new;
$r.wrap(-> |c {
my $key := c.gist;
- %cache.EXISTS-KEY($key)
- ?? %cache{$key}
- !! (%cache{$key} := callsame);
+ if %cache.EXISTS-KEY($key) {
+ %cache{$key}
+ }
+ else {
+ my &target = nextcallee;
+ $lock.protect: { %cache{$key} := target(|c) }
+ }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment