Skip to content

Commit

Permalink
added experimental cache helper to Mojolicious::Plugin::DefaultHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 5, 2010
1 parent 6cd15de commit 15b09f9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -9,6 +9,8 @@ This file documents the revision history for Perl extension Mojolicious.
Mojolicious::Guides::Cookbook recipes for more information.
- Added EXPERIMENTAL support for --mode and --home options to all
Mojolicious commands.
- Added EXPERIMENTAL cache helper to
Mojolicious::Plugin::DefaultHelpers.
- Added EXPERIMENTAL write, write_chunk and rendered methods to
Mojolicious::Controller.
- Added while and until methods for Mojo::DOM collections. (vti)
Expand Down
57 changes: 57 additions & 0 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -15,6 +15,27 @@ sub register {
# Add "app" helper
$app->renderer->add_helper(app => sub { shift->app });

# Add "cache" helper
my $cache = {};
$app->renderer->add_helper(
cache => sub {
shift;

# Callback
my $cb = pop;
return '' unless ref $cb && ref $cb eq 'CODE';

# Name
my $name = pop || join '', map { $_ || '' } caller(1);

# Cached
return $cache->{$name} if exists $cache->{$name};

# Cache
$cache->{$name} = $cb->();
}
);

# Add "content" helper
$app->renderer->add_helper(content => sub { shift->render_inner(@_) });

Expand Down Expand Up @@ -77,44 +98,80 @@ L<Mojolicious>.
=over 4
=item cache
<%= cache begin %>
<%= time %>
<% end %>
<%= cache foo => begin %>
<%= time %>
<% end %>
Cache block result in memory and prevent future execution.
Note that this helper is EXPERIMENTAL and might change without warning!
=item content
<%= content %>
Insert content into a layout template.
=item dumper
<%= dumper $foo %>
Dump a Perl data structure using L<Data::Dumper>.
=item extends
<% extends 'foo'; %>
Extend a template.
=item flash
<%= flash 'foo' %>
Access flash values.
=item include
<%= include 'menubar' %>
<%= include 'menubar', format => 'txt' %>
Include a partial template.
=item layout
<% layout 'green'; %>
Render this template with a layout.
=item param
<%= param 'foo' %>
Access request parameters and routes captures.
=item session
<%= session 'foo' %>
Access session values.
=item stash
<%= stash 'foo' %>
<% stash foo => 'bar'; %>
Access stash values.
=item url_for
<%= url_for %>
<%= url_for 'index' %>
<%= url_for 'index', foo => 'bar' %>
Generate URLs.
=back
Expand Down
33 changes: 32 additions & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -14,7 +14,7 @@ use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 536;
plan tests => 551;

# Pollution
123 =~ m/(\d+)/;
Expand Down Expand Up @@ -266,6 +266,9 @@ get '/layout' => sub {
# POST /template
post '/template' => 'index';

# GET /cached
get '/cached' => 'cached';

# * /something
any '/something' => sub {
my $self = shift;
Expand Down Expand Up @@ -882,6 +885,23 @@ $t->post_ok('/template')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Just works!');

# GET /cached
$t->get_ok('/cached')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/\d+a\d+b\d+c\d+/);
my $cached = $t->tx->res->body;

# GET /cached
$t->get_ok('/cached')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is($cached);

# GET /cached
$t->get_ok('/cached')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is($cached);

# GET /something
$t->get_ok('/something')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down Expand Up @@ -1294,6 +1314,17 @@ text!
@@ template.txt.epl
<div id="foo">Redirect works!</div>
@@ cached.html.ep
<%= cache begin =%>
<%= time =%>
<% end =%>
<%= cache begin =%><%= 'a' . int(rand(999)) %><% end =%><%= cache begin =%>
<%= 'b' . int(rand(999)) =%>
<% end =%>
<%= cache test => begin =%>
<%= 'c' . time . int(rand(999)) =%>
<% end =%>
@@ test(test)(\Qtest\E)(.html.ep
<%= $self->match->endpoint->name %>
Expand Down

0 comments on commit 15b09f9

Please sign in to comment.