Live app test, Cache::Store::Memory
Yuval Kogman [Mon, 19 Jun 2006 20:25:01 +0000 (20:25 +0000)]
lib/Catalyst/Plugin/Cache.pm
lib/Catalyst/Plugin/Cache/Backend/Memory.pm [new file with mode: 0644]
lib/Catalyst/Plugin/Cache/Store.pod [new file with mode: 0644]
lib/Catalyst/Plugin/Cache/Store/Memory.pm [new file with mode: 0644]
t/basic.t
t/key_regexes.t
t/lib/CacheTestApp.pm [new file with mode: 0644]
t/lib/CacheTestApp/Controller/Root.pm [new file with mode: 0644]
t/live_app.t [new file with mode: 0644]

index 15a73fa..6f0ad4f 100644 (file)
@@ -51,8 +51,9 @@ sub setup_cache_backends {
         $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 || {} ) };
     }
 }
 
diff --git a/lib/Catalyst/Plugin/Cache/Backend/Memory.pm b/lib/Catalyst/Plugin/Cache/Backend/Memory.pm
new file mode 100644 (file)
index 0000000..1e9dd87
--- /dev/null
@@ -0,0 +1,41 @@
+#!/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
+
+
diff --git a/lib/Catalyst/Plugin/Cache/Store.pod b/lib/Catalyst/Plugin/Cache/Store.pod
new file mode 100644 (file)
index 0000000..65c8528
--- /dev/null
@@ -0,0 +1,21 @@
+=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
diff --git a/lib/Catalyst/Plugin/Cache/Store/Memory.pm b/lib/Catalyst/Plugin/Cache/Store/Memory.pm
new file mode 100644 (file)
index 0000000..0c817c9
--- /dev/null
@@ -0,0 +1,33 @@
+#!/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
+
+
index 8e20614..2cc7e1e 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -8,6 +8,8 @@ use Test::Exception;
 
 use ok "Catalyst::Plugin::Cache";
 
+use Catalyst::Plugin::Cache::Backend::Memory;
+
 {
     package MockApp;
     use base qw/Catalyst::Plugin::Cache/;
@@ -16,14 +18,6 @@ use ok "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;
@@ -32,8 +26,8 @@ my $c = bless {}, "MockApp";
 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" );
 
@@ -75,7 +69,7 @@ is( $c->cache_get("foo"), "bar", "set" );
 $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" );
index 8ba8367..afeb79e 100644 (file)
@@ -8,6 +8,8 @@ use Test::More 'no_plan';
 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/;
@@ -21,23 +23,15 @@ use ok "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" );
diff --git a/t/lib/CacheTestApp.pm b/t/lib/CacheTestApp.pm
new file mode 100644 (file)
index 0000000..e8b47cc
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+
+package CacheTestApp;
+
+use strict;
+use warnings;
+
+use Catalyst qw/
+    Cache
+    Cache::Store::Memory
+/;
+
+__PACKAGE__->setup;
+
+__PACKAGE__;
+
+__END__
diff --git a/t/lib/CacheTestApp/Controller/Root.pm b/t/lib/CacheTestApp/Controller/Root.pm
new file mode 100644 (file)
index 0000000..836983b
--- /dev/null
@@ -0,0 +1,25 @@
+#!/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__
diff --git a/t/live_app.t b/t/live_app.t
new file mode 100644 (file)
index 0000000..3e00b72
--- /dev/null
@@ -0,0 +1,27 @@
+#!/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");