/safe_cached.patch Secret
Created
June 29, 2021 21:02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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