Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonswar committed Feb 16, 2011
1 parent 5666b2d commit 35b3b6b
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/Mason/Plugin/Cache/Component.pm
@@ -0,0 +1,32 @@
package Mason::Plugin::Cache::Component;
use Mason::PluginRole;

my %memoized;

sub cache_memoized {
my $class = shift;
if (@_) { $memoized{$class} = $_[0] }
return $memoized{$class};
}

method cache_defaults () { $self->cmeta->interp->cache_defaults }
method cache_root_class () { $self->cmeta->interp->cache_root_class }
method cache_namespace () { $self->cmeta->path }

method cache () {
if ( !@_ && $self->cache_memoized ) {
return $self->cache_memoized;
}
my $cache_root_class = $self->cache_root_class;
my %options = ( %{ $self->cache_defaults }, @_ );
if ( !exists( $options{namespace} ) ) {
$options{namespace} = $self->cache_namespace;
}
my $cache = $cache_root_class->new(%options);
if ( !@_ ) {
$self->cache_memoized($cache);
}
return $cache;
}

1;
12 changes: 12 additions & 0 deletions lib/Mason/Plugin/Cache/Filters.pm
@@ -0,0 +1,12 @@
package Mason::Plugin::Cache::Filters;
use Mason::PluginRole;

method Cache ( $key, $set_options, %cache_options ) {
Mason::DynamicFilter->new(
filter => sub {
$self->cache(%cache_options)->compute( $key, $_[0], $set_options );
}
);
}

1;
18 changes: 18 additions & 0 deletions lib/Mason/Plugin/Cache/Interp.pm
@@ -0,0 +1,18 @@
package Mason::Plugin::Cache::Interp;
use CHI;
use Mason::Util qw(catdir);
use Mason::PluginRole;

# Passed attributes
#
has 'cache_defaults' => ( isa => 'HashRef', lazy_build => 1 );
has 'cache_root_class' => ( isa => 'Str', default => 'CHI' );

method _build_cache_defaults () {
return {
driver => 'File',
root_dir => catdir( $self->data_dir, 'cache' )
};
}

1;
91 changes: 91 additions & 0 deletions lib/Mason/Plugin/Cache/t/Basic.pm
@@ -0,0 +1,91 @@
package Mason::Plugin::Cache::t::Basic;
use Test::Class::Most parent => 'Mason::Test::Class';

__PACKAGE__->default_plugins( [ '@Default', 'Cache' ] );

sub test_cache_defaults : Test(2) {
my $self = shift;
$self->run_test_in_comp(
path => '/cache/defaults.m',
test => sub {
my $comp = shift;
is( $comp->cache->label, 'File', 'cache->label' );
is( $comp->cache->namespace, $comp->cmeta->path, 'cache->namespace' );
}
);
}

sub test_cache_method : Test(1) {
my $self = shift;
$self->test_comp(
path => '/cache.m',
src => '
<%shared>
$.count => 0
</%shared>
<%method getset ($key)>
<%perl>$.count($.count+1);</%perl>
<% $.cache->compute($key, sub { $key . $.count }) %>
</%method>
namespace: <% $.cache->namespace %>
<% $.getset("foo") %>
<% $.getset("bar") %>
<% $.getset("bar") %>
<% $.getset("foo") %>
',
expect => '
namespace: /cache.m
foo1
bar2
bar2
foo1
',
);
}

sub test_cache_filter : Test(2) {
my $self = shift;

$self->test_comp(
src => '
% my $i = 1;
% foreach my $key (qw(foo bar)) {
<% $.Repeat(3), $.Cache($key) { %>
i = <% $i++ %>
</%>
% }
',
expect => '
i = 1
i = 1
i = 1
i = 2
i = 2
i = 2
',
);

$self->test_comp(
src => '
% my $i = 1;
% foreach my $key (qw(foo foo)) {
<% $.Cache($key), $.Repeat(3) { %>
i = <% $i++ %>
</%>
% }
',
expect => '
i = 1
i = 2
i = 3
i = 1
i = 2
i = 3
'
);
}
3 changes: 3 additions & 0 deletions t/Basic.t
@@ -0,0 +1,3 @@
#!perl -w
use Mason::Plugin::Cache::t::Basic;
Mason::Plugin::Cache::t::Basic->runtests;

0 comments on commit 35b3b6b

Please sign in to comment.