Skip to content

Commit

Permalink
improved Mojo::Cache to allow caching to be disabled (closes #682)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 26, 2014
1 parent eada279 commit 1ef444e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@
- Added extracting attribute to Mojo::UserAgent::CookieJar.
- Improved performance of next, next_sibling, previous and previous_sibling
methods in Mojo::DOM significantly.
- Improved Mojo::Cache to allow caching to be disabled. (mvgrimes, sri)

5.44 2014-09-23
- Fixed bug in Mojolicious::Renderer that prevented proxy objects from being
Expand Down
7 changes: 5 additions & 2 deletions lib/Mojo/Cache.pm
Expand Up @@ -8,9 +8,11 @@ sub get { (shift->{cache} || {})->{shift()} }
sub set {
my ($self, $key, $value) = @_;

return $self unless (my $max = $self->max_keys) > 0;

my $cache = $self->{cache} ||= {};
my $queue = $self->{queue} ||= [];
delete $cache->{shift @$queue} while @$queue >= $self->max_keys;
delete $cache->{shift @$queue} while @$queue >= $max;
push @$queue, $key unless exists $cache->{$key};
$cache->{$key} = $value;

Expand Down Expand Up @@ -46,7 +48,8 @@ L<Mojo::Cache> implements the following attributes.
my $max = $cache->max_keys;
$cache = $cache->max_keys(50);
Maximum number of cache keys, defaults to C<100>.
Maximum number of cache keys, defaults to C<100>. Setting the value to C<0>
will disable caching.
=head1 METHODS
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -894,6 +894,8 @@ Magnus Holm
Maik Fischer
Mark Grimes
Mark Stosberg
Marty Tennison
Expand Down
14 changes: 6 additions & 8 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -86,17 +86,15 @@ sub match {
my $match = Mojolicious::Routes::Match->new(root => $self);
$c->match($match);
my $cache = $self->cache;
my $cached = $cache ? $cache->get("$method:$path:$ws") : undef;
return $match->endpoint($cached->{endpoint})->stack($cached->{stack})
if $cached;
if (my $result = $cache->get("$method:$path:$ws")) {
return $match->endpoint($result->{endpoint})->stack($result->{stack});
}

# Check routes
$match->match($c => {method => $method, path => $path, websocket => $ws});

# Cache routes without conditions
return unless $cache && (my $endpoint = $match->endpoint);
my $result = {endpoint => $endpoint, stack => $match->stack};
$cache->set("$method:$path:$ws" => $result) unless $endpoint->has_conditions;
return unless my $endpoint = $match->endpoint;
$cache->set(
"$method:$path:$ws" => {endpoint => $endpoint, stack => $match->stack});
}

sub route {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes/Route.pm
Expand Up @@ -97,7 +97,7 @@ sub over {
my $conditions = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
return $self unless @$conditions;
$self->{over} = $conditions;
$self->root->cache(0);
$self->root->cache->max_keys(0);

return $self;
}
Expand Down
9 changes: 9 additions & 0 deletions t/mojo/cache.t
Expand Up @@ -40,4 +40,13 @@ is $cache->get('bar'), 'baz', 'right result';
is $cache->get('baz'), 'yada', 'right result';
is $cache->get('yada'), 23, 'right result';

$cache = Mojo::Cache->new(max_keys => 0);
is $cache->get('foo'), undef, 'no result';
$cache->set(foo => 'bar');
is $cache->get('foo'), undef, 'no result';
$cache = Mojo::Cache->new(max_keys => -1);
is $cache->get('foo'), undef, 'no result';
$cache->set(foo => 'bar');
is $cache->get('foo'), undef, 'no result';

done_testing();

0 comments on commit 1ef444e

Please sign in to comment.