[Catalyst-commits] r6593 - in trunk/Catalyst-Plugin-PageCache: . lib/Catalyst/Plugin t t/lib t/lib/TestAppI18N t/lib/TestAppI18N/C t/lib/TestAppI18N/I18N

andyg at dev.catalyst.perl.org andyg at dev.catalyst.perl.org
Wed Jul 25 04:04:06 GMT 2007


Author: andyg
Date: 2007-07-25 04:04:05 +0100 (Wed, 25 Jul 2007)
New Revision: 6593

Added:
   trunk/Catalyst-Plugin-PageCache/t/12i18n.t
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N.pm
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/C/
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/C/Cache.pm
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/en.pm
   trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/es.pm
Modified:
   trunk/Catalyst-Plugin-PageCache/Changes
   trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
Log:
PageCache: patch and tests from Roberto Henriquez to add i18n support

Modified: trunk/Catalyst-Plugin-PageCache/Changes
===================================================================
--- trunk/Catalyst-Plugin-PageCache/Changes	2007-07-24 15:06:02 UTC (rev 6592)
+++ trunk/Catalyst-Plugin-PageCache/Changes	2007-07-25 03:04:05 UTC (rev 6593)
@@ -1,5 +1,9 @@
 Revision history for Perl extension Catalyst::Plugin::PageCache
 
+0.17
+        - If using C::P::I18N, allow caching of different versions of a
+          single URI based on the page language. (Roberto Henríquez)
+
 0.16    2007-07-24 10:30:00
         - Fixed a bug where pages with query strings but no params
           did not have the cache key created properly.

Modified: trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm	2007-07-24 15:06:02 UTC (rev 6592)
+++ trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm	2007-07-25 03:04:05 UTC (rev 6593)
@@ -4,7 +4,7 @@
 use base qw/Class::Accessor::Fast/;
 use NEXT;
 
-our $VERSION = '0.16';
+our $VERSION = '0.17';
 
 # Do we need to cache the current page?
 __PACKAGE__->mk_accessors('_cache_page');
@@ -56,7 +56,7 @@
     my $index = $c->cache->get( "_page_cache_index" ) || {};
 
     foreach my $key ( keys %{$index} ) {
-        if ( $key =~ /^$uri$/xms ) {
+        if ( $key =~ /^(?::[^:]+:)?$uri$/xms ) {
             $c->cache->remove( $key );
             delete $index->{$key};
             $removed++;
@@ -304,6 +304,12 @@
     return $c->_page_cache_key if ( $c->_page_cache_key );
 
     my $key = "/" . $c->req->path;
+                                                                                                                          
+    # prepend language if I18N present.
+    if ( $c->can('language') ) {
+        $key = ':' . $c->language . ':' . $key;
+    }
+
     if ( scalar $c->req->param ) {
         my @params;
         foreach my $arg ( sort keys %{ $c->req->params } ) {
@@ -560,6 +566,11 @@
 
 C<setup> initializes all default values.
 
+=head1 I18N SUPPORT
+
+If your application uses L<Catalyst::Plugin::I18N> for localization, a separate cache
+key will be used for each language a page is displayed in.
+
 =head1 KNOWN ISSUES
 
 It is not currently possible to cache pages served from the Static plugin.  If you're concerned
@@ -585,6 +596,8 @@
 
 Bill Moseley, <mods at hank.org>, for many patches and tests.
 
+Roberto Henríquez, <roberto at freekeylabs.com>, for i18n support.
+
 =head1 COPYRIGHT
 
 This program is free software, you can redistribute it and/or modify it under

Added: trunk/Catalyst-Plugin-PageCache/t/12i18n.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/12i18n.t	                        (rev 0)
+++ trunk/Catalyst-Plugin-PageCache/t/12i18n.t	2007-07-25 03:04:05 UTC (rev 6593)
@@ -0,0 +1,137 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+use Test::More;
+use File::Path;
+
+
+BEGIN {
+    my $reason;
+    eval "use Catalyst::Plugin::I18N";
+    $reason .= 'Needs Catalyst::Plugin::I18N for this test. ' if $@;
+
+    eval "use Catalyst::Plugin::Cache::FileCache";
+    $reason .= 'Needs Catalyst::Plugin::Cache::FileCache for testing.' if $@;
+
+    plan $reason
+        ? ( skip_all => $reason )
+        : ( tests => 25 );
+}
+
+# remove previous cache
+rmtree 't/var' if -d 't/var';
+
+use Catalyst::Test 'TestAppI18N';
+
+run_tests();
+
+sub run_tests {
+# cache a page localized for a language
+    {
+        my $expected = 'hello 1';
+        my $request  =
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/count' );
+
+        $request->header( 'Accept-Language' => 'en' );
+
+        ok( my $response = request($request), 'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->code, 200, 'Response Code' );
+
+        is( $response->content, $expected, 'content is "hello 1"' );
+
+    }
+    
+# request the same page with same language
+    {
+        my $expected = 'hello 1';
+        my $request  =
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/count' );
+
+        $request->header( 'Accept-Language' => 'en' );
+
+        ok( my $response = request($request), 'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->code, 200, 'Response Code' );
+
+        is( $response->content, $expected, 'Content still "hello 1" from cache' );
+
+    }
+
+# request same page, different language.
+    {
+        my $expected = 'hola 2';
+        my $request  =
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/count' );
+
+        $request->header( 'Accept-Language' => 'es' );
+
+        ok( my $response = request($request), 'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->code, 200, 'Response Code' );
+
+        is( $response->content, $expected, 'Content is "hola 2"' );
+
+    }
+
+# request the same page with same language different from first...
+    {
+        my $expected = 'hola 2';
+        my $request  =
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/count' );
+
+        $request->header( 'Accept-Language' => 'es' );
+
+        ok( my $response = request($request), 'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->code, 200, 'Response Code' );
+
+        is( $response->content, $expected, 'Content still "hola 2" from cache' );
+
+    }
+
+# clearing the cached page should affect *both* languages
+    {
+        my $request = 
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/clear_cache' );
+        ok( my $response = request($request), 'request ok' );
+    }
+
+# the previous request to clear_cache also incremented the counter so we skip that one.
+
+# first ask for a fresh copy for 'en'
+    {
+        my $expected = 'hello 4';
+        my $request  =
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/count' );
+
+        $request->header( 'Accept-Language' => 'en' );
+
+        ok( my $response = request($request), 'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->code, 200, 'Response Code' );
+
+        is( $response->content, $expected, 'content is "hello 4"' );
+
+    }
+
+# next ask for a fresh copy for 'es'
+    {
+        my $expected = 'hola 5';
+        my $request  =
+            HTTP::Request->new( GET => 'http://localhost:3000/cache/count' );
+
+        $request->header( 'Accept-Language' => 'es' );
+
+        ok( my $response = request($request), 'Request' );
+        ok( $response->is_success, 'Response Successful 2xx' );
+        is( $response->code, 200, 'Response Code' );
+
+        is( $response->content, $expected, 'content is "hola 5"' );
+
+    }
+}

Added: trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/C/Cache.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/C/Cache.pm	                        (rev 0)
+++ trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/C/Cache.pm	2007-07-25 03:04:05 UTC (rev 6593)
@@ -0,0 +1,86 @@
+package TestAppI18N::C::Cache;
+
+use strict;
+use base 'Catalyst::Base';
+
+sub auto : Private {
+    my ( $self, $c ) = @_;
+    
+    $c->config->{counter}++;
+    
+    return 1;
+}
+
+sub count : Local {
+    my ( $self, $c, $expires ) = @_;
+    
+    $c->cache_page( $expires );
+    
+    $c->res->output( $c->loc('GREETING') . " " . $c->config->{counter} );
+}
+
+sub auto_count : Local {
+    my ( $self, $c ) = @_;
+    
+    $c->res->output( $c->loc('GREETING') . " " . $c->config->{counter} );
+}
+
+sub another_auto_count : Local {
+    my ( $self, $c ) = @_;
+    
+    $c->forward( 'auto_count' );
+}
+
+sub clear_cache : Local {
+    my ( $self, $c ) = @_;
+    
+    $c->clear_cached_page( '/cache/count' );
+    
+    $c->res->output( 'ok' );
+}
+
+sub clear_cache_regex : Local {
+    my ( $self, $c ) = @_;
+    
+    $c->clear_cached_page( '/cache/.*' );
+    
+    $c->res->output( 'ok' );
+}
+
+sub test_datetime : Local {
+    my ( $self, $c ) = @_;
+
+    require DateTime;
+
+    my $dt = DateTime->new( day => 24, month => 1, year => 2026, time_zone => 'UTC' );
+
+    $c->cache_page( $dt );
+
+    $c->res->output( $c->loc('GREETING') . " " . $c->config->{counter} );
+}
+
+sub extra_options : Local {
+    my ( $self, $c ) = @_;
+
+    $c->cache_page(
+        last_modified   => time,
+        expires         => 60,
+        cache_seconds   => 20,
+    );
+
+    $c->res->output( $c->loc('GREETING') . " " . $c->config->{counter} );
+}
+
+sub no_cache : Local {
+    my ( $self, $c ) = @_;
+
+    $c->cache_page(
+        last_modified   => time,
+        expires         => 0,
+        cache_seconds   => 20,
+    );
+
+    $c->res->output( $c->loc('GREETING') . " " . $c->config->{counter} );
+}
+
+1;

Added: trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/en.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/en.pm	                        (rev 0)
+++ trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/en.pm	2007-07-25 03:04:05 UTC (rev 6593)
@@ -0,0 +1,11 @@
+package TestAppI18N::I18N::en;
+use base 'TestAppI18N::I18N';
+
+use strict;
+use warnings;
+
+our %Lexicon = (
+    GREETING => 'hello',
+);
+
+1;

Added: trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/es.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/es.pm	                        (rev 0)
+++ trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N/I18N/es.pm	2007-07-25 03:04:05 UTC (rev 6593)
@@ -0,0 +1,11 @@
+package TestAppI18N::I18N::es;
+use base 'TestAppI18N::I18N';
+
+use strict;
+use warnings;
+
+our %Lexicon = (
+    GREETING => 'hola',
+);
+
+1;

Added: trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N.pm	                        (rev 0)
+++ trunk/Catalyst-Plugin-PageCache/t/lib/TestAppI18N.pm	2007-07-25 03:04:05 UTC (rev 6593)
@@ -0,0 +1,24 @@
+package TestAppI18N;
+
+use strict;
+use Catalyst;
+use Data::Dumper;
+
+our $VERSION = '0.01';
+
+TestAppI18N->config(
+    name => 'TestApp-I18N',
+    cache => {
+        storage => 't/var',
+    },
+    counter => 0,
+);
+
+TestAppI18N->setup( qw/Cache::FileCache I18N PageCache/ );
+
+sub default : Private {
+    my ( $self, $c ) = @_;
+    
+}
+
+1;




More information about the Catalyst-commits mailing list