[Catalyst-commits] r14105 -
Catalyst-Plugin-PageCache/trunk/lib/Catalyst/Plugin
t0m at dev.catalyst.perl.org
t0m at dev.catalyst.perl.org
Thu Sep 22 10:12:28 GMT 2011
Author: t0m
Date: 2011-09-22 10:12:28 +0000 (Thu, 22 Sep 2011)
New Revision: 14105
Modified:
Catalyst-Plugin-PageCache/trunk/lib/Catalyst/Plugin/PageCache.pm
Log:
Change to a role due to ordering issues with static::simple
Modified: Catalyst-Plugin-PageCache/trunk/lib/Catalyst/Plugin/PageCache.pm
===================================================================
--- Catalyst-Plugin-PageCache/trunk/lib/Catalyst/Plugin/PageCache.pm 2011-09-22 09:53:16 UTC (rev 14104)
+++ Catalyst-Plugin-PageCache/trunk/lib/Catalyst/Plugin/PageCache.pm 2011-09-22 10:12:28 UTC (rev 14105)
@@ -1,21 +1,18 @@
package Catalyst::Plugin::PageCache;
-
-use strict;
-use base qw/Class::Accessor::Fast/;
-use MRO::Compat;
+use Moose::Role;
use Digest::SHA1 ();
-our $VERSION = '0.30';
+our $VERSION = '0.31';
# Do we need to cache the current page?
-__PACKAGE__->mk_accessors('_cache_page');
+has '_cache_page' => (is => 'rw');
# Keeps track of whether the current request was served from cache
-__PACKAGE__->mk_accessors('_page_cache_used');
+has '_page_cache_used' => ( is => 'rw');
# Keeps a safe copy of the initial request parameters, in case the
# user changes them during processing
-__PACKAGE__->mk_accessors('_page_cache_key');
+has '_page_cache_key' => ( is => 'rw' );
sub cache_page {
my ( $c, @args ) = @_;
@@ -49,13 +46,13 @@
my $pc_config = $c->config->{'Plugin::PageCache'};
my $is_debug = $pc_config->{debug};
-
+
# Warn if index was disabled
my $index_page_key = $pc_config->{index_page_key} or do {
$c->log->warn("clear_cached_page($uri) did not clear the cache because disable_index is set");
return;
};
-
+
my $cache = $c->cache; # curry the cache just once, here
my $index = $cache->get( $index_page_key ) || {};
@@ -112,32 +109,32 @@
}
-sub dispatch {
- my $c = shift;
+around dispatch => sub {
+ my ($orig, $c, @args) = @_;
# only serve GET and HEAD request pages from cache
# (never POST, PUT, DELETE etc)
- return $c->next::method(@_)
+ return $c->$orig(@args)
unless $c->req->method =~ m/^(?:GET|HEAD)$/;
my $pc_config = $c->config->{'Plugin::PageCache'};
my $hook_name = $pc_config->{cache_dispatch_hook} || $pc_config->{cache_hook};
my $hook = $hook_name ? $c->can($hook_name) : undef;
- return $c->next::method(@_) if ( $hook && !$c->$hook() );
+ return $c->$orig(@args) if ( $hook && !$c->$hook() );
- return $c->next::method(@_)
+ return $c->orig(@args)
if ( $pc_config->{auto_check_user}
&& $c->can('user_exists')
&& $c->user_exists);
# check the page cache for a cached copy of this page
- return $c->next::method(@_)
+ return $c->$orig(@args)
unless my $key = $c->_get_page_cache_key;
my $cache = $c->cache; # curry the cache just once, here
- return $c->next::method(@_)
+ return $c->$orig(@args)
unless my $data = $cache->get( $key );
# Time to remove page from cache?
@@ -147,9 +144,9 @@
# Extend the expiration time for others while
# this caller refreshes the cache
$data->{expire_time} = time() + $busy_lock;
-
+
$cache->set( $key, $data );
-
+
$c->log->debug( "$key has expired, being refreshed for $busy_lock seconds" )
if ($pc_config->{debug});
}
@@ -167,7 +164,7 @@
}
}
- return $c->next::method(@_);
+ return $c->$orig(@args);
}
$c->log->debug("Serving $key from page cache, expires in "
@@ -194,7 +191,7 @@
$c->res->header('X-PageCache', 'Catalyst');
-}
+};
# See if request matches last_modified date in cache
# if so, arrange to return a 304 Not Modified status
@@ -231,7 +228,7 @@
);
}
}
-
+
return unless $pc_config->{set_http_headers};
if ( exists $data->{expires} ) {
@@ -261,27 +258,27 @@
unless $c->res->status && $c->res->status == 304;
}
-sub finalize {
- my $c = shift;
+around finalize => sub {
+ my ($orig, $c, @args) = @_;
# never cache POST requests
- return $c->next::method(@_) if ( $c->req->method eq "POST" );
+ return $c->$orig(@args) if ( $c->req->method eq "POST" );
my $pc_config = $c->config->{'Plugin::PageCache'};
my $hook_name = $pc_config->{cache_dispatch_hook} || $pc_config->{cache_hook};
my $hook = $hook_name ? $c->can($hook_name) : undef;
- return $c->next::method(@_) if ( $hook && !$c->$hook() );
+ return $c->$orig(@args) if ( $hook && !$c->$hook() );
- return $c->next::method(@_)
+ return $c->$orig(@args)
if ( $pc_config->{auto_check_user}
&& $c->can('user_exists')
&& $c->user_exists);
- return $c->next::method(@_) if ( scalar @{ $c->error } );
+ return $c->$orig(@args) if ( scalar @{ $c->error } );
# if we already served the current request from cache, we can skip the
# rest of this method
- return $c->next::method(@_) if ( $c->_page_cache_used );
+ return $c->$orig(@args) if ( $c->_page_cache_used );
if (!$c->_cache_page
&& scalar @{ $pc_config->{auto_cache} })
@@ -311,8 +308,8 @@
$c->_page_cache_not_modified( $data ) if $data;
}
- return $c->next::method(@_);
-}
+ return $c->$orig(@args);
+};
sub _store_page_in_cache {
@@ -362,7 +359,7 @@
$data->{expires} = $options->{expires}
}
- my $cache = $c->cache; # curry the cache just once, here
+ my $cache = $c->cache;
$cache->set( $key, $data );
@@ -381,11 +378,9 @@
}
-sub setup {
+after setup_finalize => sub {
my $c = shift;
- $c->next::method(@_);
-
# 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};
@@ -421,7 +416,7 @@
return;
}
- my $cache = $c->cache; # curry the cache just once, here
+ my $cache = $c->cache;
# Older Cache plugins
if ( $cache->isa('Cache::FileCache') ) {
@@ -439,11 +434,11 @@
else {
die __PACKAGE__ . " requires a Catalyst::Plugin::Cache plugin.";
}
-}
+};
sub _get_page_cache_key {
my $c = shift;
-
+
# We can't rely on the params after the user's code has run,
# so we cache the key created during the initial dispatch phase
# and reuse it at finalize time.
@@ -452,7 +447,7 @@
# override key if required, else use uri path
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') ) {
$key = ':' . $c->language . ':' . $key;
More information about the Catalyst-commits
mailing list