$app->setup_generic_cache_backend( $name, $app->get_cache_backend_config( $name ) || {} );
}
- if ( !$app->get_cache_backend("default") and my $default_config = $app->get_default_cache_backend_config) {
- $app->setup_generic_cache_backend( default => $default_config );
+ if ( !$app->get_cache_backend("default") ) {
+ local $@;
+ eval { $app->setup_generic_cache_backend( default => $app->get_default_cache_backend_config || {} ) };
}
}
--- /dev/null
+#!/usr/bin/perl
+
+package Catalyst::Plugin::Cache::Backend::Memory;
+use Storable;
+
+use strict;
+use warnings;
+
+use Storable qw/freeze thaw/;
+
+sub new { bless {}, shift }
+
+sub get { ${thaw($_[0]{$_[1]}) || return} };
+
+sub set { $_[0]{$_[1]} = freeze(\$_[2]) };
+
+sub remove { delete $_[0]{$_[1]} };
+
+__PACKAGE__;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Catalyst::Plugin::Cache::Backend::Memory - Stupid memory based caching backend.
+
+=head1 SYNOPSIS
+
+ use Catalyst::Plugin::Cache::Backend::Memory;
+
+ my $m = Catalyst::Plugin::Cache::Backend::Memory->new;
+
+ $m->set( foo => "thing" );
+
+=head1 DESCRIPTION
+
+=cut
+
+
--- /dev/null
+=pod
+
+=head1 NAME
+
+Catalyst::Plugin::Cache::Store - how to write a Cache store plugin.
+
+=head1 SYNOPSIS
+
+ package Catalyst::Plugin::Cache::Store::Frobnicator;
+
+ sub setup_frobnicator_cache_backend {
+ my ( $app, $name, $config ) = @_;
+
+ ....
+
+ $app->register_cache_backend( $name => $cache_object );
+ }
+
+=head1 DESCRIPTION
+
+=cut
--- /dev/null
+#!/usr/bin/perl
+
+package Catalyst::Plugin::Cache::Store::Memory;
+
+use strict;
+use warnings;
+
+use Catalyst::Plugin::Cache::Backend::Memory;
+
+sub setup_memory_cache_backend {
+ my ( $app, $name ) = @_;
+ $app->register_cache_backend( $name => Catalyst::Plugin::Cache::Backend::Memory->new );
+}
+
+__PACKAGE__;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Catalyst::Plugin::Cache::Store::Memory - Stupid memory based cache store plugin.
+
+=head1 SYNOPSIS
+
+ use Catalyst::Plugin::Cache::Store::Memory;
+
+=head1 DESCRIPTION
+
+=cut
+
+
use ok "Catalyst::Plugin::Cache";
+use Catalyst::Plugin::Cache::Backend::Memory;
+
{
package MockApp;
use base qw/Catalyst::Plugin::Cache/;
my %config;
sub config { \%config };
-
- package MemoryCache;
- use Storable qw/freeze thaw/;
-
- sub new { bless {}, shift }
- sub get { ${thaw($_[0]{$_[1]}) || return} };
- sub set { $_[0]{$_[1]} = freeze(\$_[2]) };
- sub remove { delete $_[0]{$_[1]} };
}
MockApp->setup;
can_ok( $c, "register_cache_backend" );
can_ok( $c, "unregister_cache_backend" );
-MockApp->register_cache_backend( default => MemoryCache->new );
-MockApp->register_cache_backend( moose => MemoryCache->new );
+MockApp->register_cache_backend( default => Catalyst::Plugin::Cache::Backend::Memory->new );
+MockApp->register_cache_backend( moose => Catalyst::Plugin::Cache::Backend::Memory->new );
can_ok( $c, "cache" );
$c->cache_remove( "foo" );
is( $c->cache_get("foo"), undef, "remove" );
-MockApp->register_cache_backend( elk => MemoryCache->new );
+MockApp->register_cache_backend( elk => Catalyst::Plugin::Cache::Backend::Memory->new );
is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->default_cache_backend, "choose default" );
is( $c->choose_cache_backend_wrapper( key => "foo", backend => "elk" ), $c->get_cache_backend("elk"), "override choice" );
use ok "Catalyst::Plugin::Cache";
use ok "Catalyst::Plugin::Cache::Choose::KeyRegexes";
+use Catalyst::Plugin::Cache::Backend::Memory;
+
{
package MockApp;
use base qw/Catalyst::Plugin::Cache Catalyst::Plugin::Cache::Choose::KeyRegexes/;
},
);
sub config { \%config }
-
- package MemoryCache;
- use Storable qw/freeze thaw/;
-
- sub new { bless {}, shift }
- sub get { ${thaw($_[0]{$_[1]}) || return} };
- sub set { $_[0]{$_[1]} = freeze(\$_[2]) };
- sub remove { delete $_[0]{$_[1]} };
}
MockApp->setup;
my $c = bless {}, "MockApp";
-MockApp->register_cache_backend( default => MemoryCache->new );
-MockApp->register_cache_backend( foo_store => MemoryCache->new );
-MockApp->register_cache_backend( bar_store => MemoryCache->new );
+MockApp->register_cache_backend( default => Catalyst::Plugin::Cache::Backend::Memory->new );
+MockApp->register_cache_backend( foo_store => Catalyst::Plugin::Cache::Backend::Memory->new );
+MockApp->register_cache_backend( bar_store => Catalyst::Plugin::Cache::Backend::Memory->new );
is( $c->choose_cache_backend_wrapper( key => "baz" ), $c->default_cache_backend, "chose default" );
is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->get_cache_backend("foo_store"), "chose foo" );
--- /dev/null
+#!/usr/bin/perl
+
+package CacheTestApp;
+
+use strict;
+use warnings;
+
+use Catalyst qw/
+ Cache
+ Cache::Store::Memory
+/;
+
+__PACKAGE__->setup;
+
+__PACKAGE__;
+
+__END__
--- /dev/null
+#!/usr/bin/perl
+
+package CacheTestApp::Controller::Root;
+use base qw/Catalyst::Controller/;
+
+use strict;
+use warnings;
+
+__PACKAGE__->config( namespace => "" );
+
+sub foo : Local {
+ my ( $self, $c ) = @_;
+
+ $c->cache->set( foo => "Foo" );
+}
+
+sub bar : Local {
+ my ( $self, $c ) = @_;
+
+ $c->res->body( $c->cache->get( "foo" ) || "not found" );
+}
+
+__PACKAGE__;
+
+__END__
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+ eval { require Test::WWW::Mechanize::Catalyst }
+ or plan skip_all =>
+ "Test::WWW::Mechanize::Catalyst is required for this test";
+
+ plan tests => 5;
+}
+
+use lib "t/lib";
+use Test::WWW::Mechanize::Catalyst "CacheTestApp";
+
+my $ua = Test::WWW::Mechanize::Catalyst->new;
+
+$ua->get_ok("/bar");
+$ua->content_is("not found");
+
+$ua->get_ok("/foo");
+
+$ua->get_ok("/bar");
+$ua->content_is("Foo");