Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support extended NFA cache API.
This supports Cursor optimizations recently made in NQP.
  • Loading branch information
jnthn committed Jul 30, 2014
1 parent 509b1a4 commit c9ad800
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Perl6/Metamodel/MethodContainer.nqp
Expand Up @@ -84,10 +84,20 @@ role Perl6::Metamodel::MethodContainer {
}

# Caches or updates a cached value.
method cache($obj, $key, $value_generator) {
method cache($obj, str $key, $value_generator) {
%!cache || (%!cache := {});
nqp::existskey(%!cache, $key) ??
%!cache{$key} !!
(%!cache{$key} := $value_generator())
}

method cache_get($obj, str $key) {
my %caches := %!cache;
nqp::ishash(%caches) ?? nqp::atkey(%caches, $key) !! nqp::null()
}

method cache_add($obj, str $key, $value) {
%!cache := nqp::hash() unless nqp::ishash(%!cache);
%!cache{$key} := $value;
}
}

0 comments on commit c9ad800

Please sign in to comment.