[Catalyst-commits] r8258 - in trunk/Catalyst-Plugin-PageCache: .
lib/Catalyst/Plugin t
andyg at dev.catalyst.perl.org
andyg at dev.catalyst.perl.org
Fri Aug 22 17:57:35 BST 2008
Author: andyg
Date: 2008-08-22 17:57:35 +0100 (Fri, 22 Aug 2008)
New Revision: 8258
Added:
trunk/Catalyst-Plugin-PageCache/t/14keymaker.t
Modified:
trunk/Catalyst-Plugin-PageCache/Changes
trunk/Catalyst-Plugin-PageCache/README
trunk/Catalyst-Plugin-PageCache/TODO
trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
trunk/Catalyst-Plugin-PageCache/t/05expires.t
trunk/Catalyst-Plugin-PageCache/t/06auto_cache.t
trunk/Catalyst-Plugin-PageCache/t/07set_http_headers.t
trunk/Catalyst-Plugin-PageCache/t/09datetime.t
trunk/Catalyst-Plugin-PageCache/t/10options.t
trunk/Catalyst-Plugin-PageCache/t/11nocache.t
trunk/Catalyst-Plugin-PageCache/t/13cachehook.t
Log:
PageCache 0.19, key_maker support and config namespace change
Modified: trunk/Catalyst-Plugin-PageCache/Changes
===================================================================
--- trunk/Catalyst-Plugin-PageCache/Changes 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/Changes 2008-08-22 16:57:35 UTC (rev 8258)
@@ -1,5 +1,10 @@
Revision history for Perl extension Catalyst::Plugin::PageCache
+0.19 2008-08-22 13:00:00
+ - Change config namespace to $c->config->{'Plugin::PageCache'}, old namespace will
+ still continue to work.
+ - key_maker method, allows custom cache keys to be used. (Martin Ellison)
+
0.18 2008-04-25 11:30:00
- cache_hook method, run a method before returning a cached page.
(J. Shirley)
Modified: trunk/Catalyst-Plugin-PageCache/README
===================================================================
--- trunk/Catalyst-Plugin-PageCache/README 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/README 2008-08-22 16:57:35 UTC (rev 8258)
@@ -5,22 +5,24 @@
use Catalyst;
MyApp->setup( qw/Cache::FileCache PageCache/ );
- MyApp->config->{page_cache} = {
- expires => 300,
- set_http_headers => 1,
- auto_cache => [
- '/view/.*',
- '/list',
- ],
- debug => 1,
- # Optionally, a cache hook to be called prior to dispatch to
- # determine if the page should be cached. This is called both
- # before dispatch, and before finalize.
- cache_hook => 'some_method'
- };
+ __PACKAGE__->config(
+ 'Plugin::PageCache' => {
+ expires => 300,
+ set_http_headers => 1,
+ auto_cache => [
+ '/view/.*',
+ '/list',
+ ],
+ debug => 1,
+ # Optionally, a cache hook to be called prior to dispatch to
+ # determine if the page should be cached. This is called both
+ # before dispatch, and before finalize.
+ cache_hook => 'some_method'
+ }
+ );
sub some_method {
- my ( $c ) = @_;
+ my $c = shift;
if ( $c->user_exists and $c->user->some_field ) {
return 0; # Don't cache
}
@@ -127,6 +129,17 @@
Note that this is called BEFORE auto_check_user, so you have more
flexibility to determine what to do for not logged in users.
+ To override the generation of page keys:
+
+ __PACKAGE__->config(
+ 'Plugin::PageCache' => {
+ key_maker => sub {
+ my $c = shift;
+ return $c->req->base . '/' . $c->req->path;
+ }
+ }
+ );
+
METHODS
cache_page
Call cache_page in any controller method you wish to be cached.
Modified: trunk/Catalyst-Plugin-PageCache/TODO
===================================================================
--- trunk/Catalyst-Plugin-PageCache/TODO 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/TODO 2008-08-22 16:57:35 UTC (rev 8258)
@@ -1,2 +1 @@
- disable index stuff by default, make regex clear_cached_page an option
-- apply patch to support key prefix
Modified: trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm 2008-08-22 16:57:35 UTC (rev 8258)
@@ -4,7 +4,7 @@
use base qw/Class::Accessor::Fast/;
use NEXT;
-our $VERSION = '0.18';
+our $VERSION = '0.19';
# Do we need to cache the current page?
__PACKAGE__->mk_accessors('_cache_page');
@@ -24,7 +24,7 @@
if ( ref($args[0]) eq 'HASH' || @args > 1 ) {
my $options = ref( $args[0] ) ? shift : { @args };
- $options->{cache_seconds} = $c->config->{page_cache}->{expires}
+ $options->{cache_seconds} = $c->config->{'Plugin::PageCache'}->{expires}
unless exists $options->{cache_seconds};
$c->_cache_page( $options );
@@ -37,9 +37,8 @@
$expires = $expires->epoch - time
if ref($expires) && $expires->isa('DateTime');
+ $expires ||= $c->config->{'Plugin::PageCache'}->{expires};
- $expires ||= $c->config->{page_cache}->{expires};
-
# mark the page for caching during finalize
if ( $expires > 0 ) {
$c->_cache_page( { cache_seconds => $expires } );
@@ -50,6 +49,8 @@
my ( $c, $uri ) = @_;
return unless ( $c->can( 'cache' ) );
+
+ my $is_debug = $c->config->{'Plugin::PageCache'}->{debug};
my $removed = 0;
@@ -60,12 +61,18 @@
$c->cache->remove( $key );
delete $index->{$key};
$removed++;
- $c->log->debug( "Removed $key from page cache" )
- if ( $c->config->{page_cache}->{debug} );
+
+ $c->log->debug( "Removed $key from page cache" ) if $is_debug;
}
}
- $c->cache->set( "_page_cache_index", $index,
- $c->config->{page_cache}->{no_expire} ) if ( $removed );
+
+ if ( $removed ) {
+ $c->cache->set(
+ "_page_cache_index",
+ $index,
+ $c->config->{'Plugin::PageCache'}->{no_expire}
+ );
+ }
}
sub dispatch {
@@ -74,15 +81,17 @@
# never serve POST request pages from cache
return $c->NEXT::dispatch(@_) if ( $c->req->method eq "POST" );
- my $hook = $c->config->{page_cache}->{cache_hook} ?
- $c->can($c->config->{page_cache}->{cache_hook}) : undef;
+ my $hook =
+ $c->config->{'Plugin::PageCache'}->{cache_hook}
+ ? $c->can($c->config->{'Plugin::PageCache'}->{cache_hook})
+ : undef;
+
return $c->NEXT::dispatch(@_) if ( $hook && !$c->$hook() );
- return $c->NEXT::dispatch(@_) if (
- $c->config->{page_cache}->{auto_check_user} &&
- $c->can('user_exists') &&
- $c->user_exists
- );
+ return $c->NEXT::dispatch(@_)
+ if ( $c->config->{'Plugin::PageCache'}->{auto_check_user}
+ && $c->can('user_exists')
+ && $c->user_exists);
# check the page cache for a cached copy of this page
return $c->NEXT::dispatch(@_)
@@ -95,30 +104,28 @@
if ( $data->{expire_time} and $data->{expire_time} <= time ) {
$c->log->debug( "Expiring $key from page cache" )
- if ( $c->config->{page_cache}->{debug} );
+ if ($c->config->{'Plugin::PageCache'}->{debug});
$c->cache->remove( $key );
my $index = $c->cache->get( "_page_cache_index" ) || {};
delete $index->{$key};
$c->cache->set( "_page_cache_index", $index,
- $c->config->{page_cache}->{no_expire} );
+ $c->config->{'Plugin::PageCache'}->{no_expire});
return $c->NEXT::dispatch(@_);
}
- $c->log->debug( "Serving $key from page cache, expires in " .
- ( $data->{expire_time} - time ) . " seconds" )
- if ( $c->config->{page_cache}->{debug} );
+ $c->log->debug("Serving $key from page cache, expires in "
+ . ($data->{expire_time} - time)
+ . " seconds")
+ if ($c->config->{'Plugin::PageCache'}->{debug});
$c->_page_cache_used( 1 );
-
# Check If-Modified-Since headers
return 1 if $c->_page_cache_not_modified( $data );
-
-
# Serve cached page
$c->res->body( $data->{body} );
@@ -126,11 +133,9 @@
$c->res->content_type( join '; ', @{$data->{content_type}} )
if $data->{content_type};
-
$c->res->content_encoding( $data->{content_encoding} )
if $data->{content_encoding};
-
$c->_set_page_cache_headers( $data );
$c->res->header('X-PageCache', 'Catalyst');
@@ -157,15 +162,13 @@
return;
}
-
# Sets cache headers for the page if set_http_headers is true.
sub _set_page_cache_headers {
my ( $c, $data ) = @_;
- return unless $c->config->{page_cache}->{set_http_headers};
+ return unless $c->config->{'Plugin::PageCache'}->{set_http_headers};
-
if ( exists $data->{expires} ) {
# page cache but not client cache
@@ -175,15 +178,16 @@
return;
}
- $c->res->headers->header( 'Cache-Control' =>
- "max-age=" . $data->{expires} );
+ $c->res->headers->header(
+ 'Cache-Control' => "max-age=" . $data->{expires});
$c->res->headers->expires( time + $data->{expires} );
- } else {
+ }
+ else {
- $c->res->headers->header( 'Cache-Control' =>
- "max-age=" . ( $data->{expire_time} - time ) );
+ $c->res->headers->header(
+ 'Cache-Control' => "max-age=" . ($data->{expire_time} - time));
$c->res->headers->expires( $data->{expire_time} );
}
@@ -198,33 +202,37 @@
# never cache POST requests
return $c->NEXT::finalize(@_) if ( $c->req->method eq "POST" );
- my $hook = $c->config->{page_cache}->{cache_hook} ?
- $c->can($c->config->{page_cache}->{cache_hook}) : undef;
+ my $hook =
+ $c->config->{'Plugin::PageCache'}->{cache_hook}
+ ? $c->can($c->config->{'Plugin::PageCache'}->{cache_hook})
+ : undef;
return $c->NEXT::finalize(@_) if ( $hook && !$c->$hook() );
-
- return $c->NEXT::finalize(@_) if (
- $c->config->{page_cache}->{auto_check_user} &&
- $c->can('user_exists') &&
- $c->user_exists
- );
+ return $c->NEXT::finalize(@_)
+ if ( $c->config->{'Plugin::PageCache'}->{auto_check_user}
+ && $c->can('user_exists')
+ && $c->user_exists);
return $c->NEXT::finalize(@_) if ( scalar @{ $c->error } );
# if we already served the current request from cache, we can skip the
# rest of this method
return $c->NEXT::finalize(@_) if ( $c->_page_cache_used );
- if ( !$c->_cache_page && scalar @{ $c->config->{page_cache}->{auto_cache} } ) {
+ if (!$c->_cache_page
+ && scalar @{ $c->config->{'Plugin::PageCache'}->{auto_cache} })
+ {
+
# is this page part of the auto_cache list?
my $path = "/" . $c->req->path;
# For performance, this should be moved to setup, and generate a hash.
AUTO_CACHE:
- foreach my $auto ( @{ $c->config->{page_cache}->{auto_cache} } ) {
+ foreach my $auto (@{ $c->config->{'Plugin::PageCache'}->{auto_cache} })
+ {
next if $auto =~ m/^\d$/;
if ( $path =~ /^$auto$/ ) {
$c->log->debug( "Auto-caching page $path" )
- if ( $c->config->{page_cache}->{debug} );
+ if ($c->config->{'Plugin::PageCache'}->{debug});
$c->cache_page;
last AUTO_CACHE;
}
@@ -236,17 +244,19 @@
my $now = time;
+ $c->log->debug(
+ "Caching page $key for $options->{cache_seconds} seconds"
+ ) if ($c->config->{'Plugin::PageCache'}->{debug});
- $c->log->debug( "Caching page $key for $options->{cache_seconds} seconds" )
- if ( $c->config->{page_cache}->{debug} );
-
# Cache some additional metadata along with the content
# Some caches don't support expirations, so we do it manually
my $data = {
body => $c->res->body || undef,
content_type => [ $c->res->content_type ] || undef,
content_encoding => $c->res->content_encoding || undef,
- create_time => $options->{last_modified} || $c->res->headers->last_modified || $now,
+ create_time => $options->{last_modified}
+ || $c->res->headers->last_modified
+ || $now,
expire_time => $now + $options->{cache_seconds},
};
@@ -256,7 +266,6 @@
$c->_set_page_cache_headers( $data ); # don't forget the first time
-
# Keep an index cache of all pages that have been cached, for use
# with clear_cached_page
@@ -265,7 +274,7 @@
# Save date in cache
$c->cache->set( "_page_cache_index", $index,
- $c->config->{page_cache}->{no_expire} );
+ $c->config->{'Plugin::PageCache'}->{no_expire});
# Check for If-Modified-Since
$c->_page_cache_not_modified( $data );
@@ -279,11 +288,16 @@
$c->NEXT::setup(@_);
- $c->config->{page_cache}->{auto_cache} ||= [];
- $c->config->{page_cache}->{expires} ||= 60 * 5;
- $c->config->{page_cache}->{set_http_headers} ||= 0;
- $c->config->{page_cache}->{debug} ||= $c->debug;
+ # allow code using old config key to work
+ if ( $c->config->{page_cache} and !$c->config->{'Plugin::PageCache'} ) {
+ $c->config->{'Plugin::PageCache'} = delete $c->config->{page_cache};
+ }
+ $c->config->{'Plugin::PageCache'}->{auto_cache} ||= [];
+ $c->config->{'Plugin::PageCache'}->{expires} ||= 60 * 5;
+ $c->config->{'Plugin::PageCache'}->{set_http_headers} ||= 0;
+ $c->config->{'Plugin::PageCache'}->{debug} ||= $c->debug;
+
# detect the cache plugin being used and set appropriate
# never-expires syntax
if ( $c->can('cache') ) {
@@ -295,13 +309,15 @@
# Older Cache plugins
if ( $c->cache->isa('Cache::FileCache') ) {
- $c->config->{page_cache}->{no_expire} = "never";
+ $c->config->{'Plugin::PageCache'}->{no_expire} = "never";
}
- elsif ( $c->cache->isa('Cache::Memcached') ||
- $c->cache->isa('Cache::FastMmap') ) {
+ elsif ($c->cache->isa('Cache::Memcached')
+ || $c->cache->isa('Cache::FastMmap'))
+ {
+
# Memcached defaults to 'never' when not given an expiration
# In FastMmap, it's not possible to set an expiration
- $c->config->{page_cache}->{no_expire} = undef;
+ $c->config->{'Plugin::PageCache'}->{no_expire} = undef;
}
}
else {
@@ -316,7 +332,9 @@
# use the key created during the initial dispatch phase
return $c->_page_cache_key if ( $c->_page_cache_key );
- my $key = "/" . $c->req->path;
+ # override key if required
+ my $keymaker = $c->config->{'Plugin::PageCache'}->{key_maker};
+ my $key = $keymaker ? $keymaker->($c) : "/" . $c->req->path;
# prepend language if I18N present.
if ( $c->can('language') ) {
@@ -357,22 +375,24 @@
use Catalyst;
MyApp->setup( qw/Cache::FileCache PageCache/ );
- MyApp->config->{page_cache} = {
- expires => 300,
- set_http_headers => 1,
- auto_cache => [
- '/view/.*',
- '/list',
- ],
- debug => 1,
- # Optionally, a cache hook to be called prior to dispatch to
- # determine if the page should be cached. This is called both
- # before dispatch, and before finalize.
- cache_hook => 'some_method'
- };
+ __PACKAGE__->config(
+ 'Plugin::PageCache' => {
+ expires => 300,
+ set_http_headers => 1,
+ auto_cache => [
+ '/view/.*',
+ '/list',
+ ],
+ debug => 1,
+ # Optionally, a cache hook to be called prior to dispatch to
+ # determine if the page should be cached. This is called both
+ # before dispatch, and before finalize.
+ cache_hook => 'some_method'
+ }
+ );
sub some_method {
- my ( $c ) = @_;
+ my $c = shift;
if ( $c->user_exists and $c->user->some_field ) {
return 0; # Don't cache
}
@@ -479,6 +499,17 @@
Note that this is called BEFORE auto_check_user, so you have more flexibility
to determine what to do for not logged in users.
+To override the generation of page keys:
+
+ __PACKAGE__->config(
+ 'Plugin::PageCache' => {
+ key_maker => sub {
+ my $c = shift;
+ return $c->req->base . '/' . $c->req->path;
+ }
+ }
+ );
+
=head1 METHODS
=head2 cache_page
Modified: trunk/Catalyst-Plugin-PageCache/t/05expires.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/05expires.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/05expires.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -21,7 +21,7 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{expires} = 2;
+TestApp->config->{'Plugin::PageCache'}->{expires} = 2;
# cache a page
ok( my $res = request('http://localhost/cache/count'), 'request ok' );
Modified: trunk/Catalyst-Plugin-PageCache/t/06auto_cache.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/06auto_cache.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/06auto_cache.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -21,7 +21,7 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{auto_cache} = [
+TestApp->config->{'Plugin::PageCache'}->{auto_cache} = [
'/cache/auto_count',
'/cache/another.+',
];
Modified: trunk/Catalyst-Plugin-PageCache/t/07set_http_headers.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/07set_http_headers.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/07set_http_headers.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -21,7 +21,7 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{set_http_headers} = 1;
+TestApp->config->{'Plugin::PageCache'}->{set_http_headers} = 1;
# cache a page
my $cache_time = time;
Modified: trunk/Catalyst-Plugin-PageCache/t/09datetime.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/09datetime.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/09datetime.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -30,7 +30,7 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{set_http_headers} = 1;
+TestApp->config->{'Plugin::PageCache'}->{set_http_headers} = 1;
# cache a page
my $cache_time = time;
Modified: trunk/Catalyst-Plugin-PageCache/t/10options.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/10options.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/10options.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -23,7 +23,7 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{set_http_headers} = 1;
+TestApp->config->{'Plugin::PageCache'}->{set_http_headers} = 1;
# cache a page
my $cache_time = time;
Modified: trunk/Catalyst-Plugin-PageCache/t/11nocache.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/11nocache.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/11nocache.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -23,7 +23,7 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{set_http_headers} = 1;
+TestApp->config->{'Plugin::PageCache'}->{set_http_headers} = 1;
# cache a page
my $cache_time = time;
Modified: trunk/Catalyst-Plugin-PageCache/t/13cachehook.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/13cachehook.t 2008-08-22 14:45:07 UTC (rev 8257)
+++ trunk/Catalyst-Plugin-PageCache/t/13cachehook.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -24,8 +24,8 @@
use Catalyst::Test 'TestApp';
# add config option
-TestApp->config->{page_cache}->{set_http_headers} = 1;
-TestApp->config->{page_cache}->{cache_hook} = 'use_pagecache';
+TestApp->config->{'Plugin::PageCache'}->{set_http_headers} = 1;
+TestApp->config->{'Plugin::PageCache'}->{cache_hook} = 'use_pagecache';
local *TestApp::use_pagecache = sub { 0 };
cmp_ok( TestApp->use_pagecache(), '==', '0' );
Added: trunk/Catalyst-Plugin-PageCache/t/14keymaker.t
===================================================================
--- trunk/Catalyst-Plugin-PageCache/t/14keymaker.t (rev 0)
+++ trunk/Catalyst-Plugin-PageCache/t/14keymaker.t 2008-08-22 16:57:35 UTC (rev 8258)
@@ -0,0 +1,47 @@
+#!perl
+
+use strict;
+use warnings;
+no warnings 'redefine';
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+use Test::More;
+use File::Path;
+
+BEGIN {
+ eval "use Catalyst::Plugin::Cache::FileCache";
+ plan $@
+ ? ( skip_all => 'needs Catalyst::Plugin::Cache::FileCache for testing' )
+ : ( tests => 8 );
+}
+
+# This test that options can be passed to cache.
+
+# remove previous cache
+rmtree 't/var' if -d 't/var';
+
+use Catalyst::Test 'TestApp';
+
+# add config option
+# cannot call TestApp->config() because TestApp has already called setup
+TestApp->config->{'Plugin::PageCache'}->{key_maker} = sub {
+ my ($c) = @_;
+ return $c->req->base . q{/} . $c->req->path;
+};
+
+# cache a page
+ok( my $res = request('http://host1/cache/count'), 'request ok' );
+is( $res->content, 1, 'count is 1' );
+
+# page will be served from cache
+ok( $res = request('http://host1/cache/count'), 'request ok' );
+is( $res->content, 1, 'count is still 1 from cache' );
+
+# page will not be served from cache
+ok( $res = request('http://host2/cache/count'), 'request ok' );
+is( $res->content, 2, 'count is 2 from cache' );
+
+# page will be served from cache
+ok( $res = request('http://host2/cache/count'), 'request ok' );
+is( $res->content, 2, 'count is still 2 from cache' );
Property changes on: trunk/Catalyst-Plugin-PageCache/t/14keymaker.t
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the Catalyst-commits
mailing list