[Catalyst-commits] r13670 - in trunk/Catalyst-Plugin-PageCache: .
lib/Catalyst/Plugin
timbunce at dev.catalyst.perl.org
timbunce at dev.catalyst.perl.org
Mon Nov 1 12:13:31 GMT 2010
Author: timbunce
Date: 2010-11-01 12:13:30 +0000 (Mon, 01 Nov 2010)
New Revision: 13670
Modified:
trunk/Catalyst-Plugin-PageCache/Changes
trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
Log:
Eliminated repeated common subexpression $c->config->{'Plugin::PageCache'}
Eliminated repeated common subexpression $c->res->headers
Modified: trunk/Catalyst-Plugin-PageCache/Changes
===================================================================
--- trunk/Catalyst-Plugin-PageCache/Changes 2010-11-01 12:01:55 UTC (rev 13669)
+++ trunk/Catalyst-Plugin-PageCache/Changes 2010-11-01 12:13:30 UTC (rev 13670)
@@ -7,6 +7,7 @@
- XXX Test 15busy_lock.t fails as the moment.
- Updated test app code to avoid deprecated constructs.
- Only serve GET and HEAD requests (instead of all except POST). RT#53307.
+ - Assorted performance optimizations.
0.22 2009-06-25 10:38:00
- Update to use MRO::Compat
Modified: trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm 2010-11-01 12:01:55 UTC (rev 13669)
+++ trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm 2010-11-01 12:13:30 UTC (rev 13670)
@@ -49,14 +49,16 @@
my ( $c, $uri ) = @_;
return unless ( $c->can( 'cache' ) );
+
+ my $config_PageCache = $c->config->{'Plugin::PageCache'};
# Warn if index was disabled
- if ( $c->config->{'Plugin::PageCache'}->{disable_index} ) {
+ if ( $config_PageCache->{disable_index} ) {
$c->log->warn("Warning: clear_cached_page($uri) did not clear the cache, disable_index is set");
return;
}
- my $is_debug = $c->config->{'Plugin::PageCache'}->{debug};
+ my $is_debug = $config_PageCache->{debug};
my $removed = 0;
@@ -78,7 +80,7 @@
$cache->set(
"_page_cache_index",
$index,
- $c->config->{'Plugin::PageCache'}->{no_expire}
+ $config_PageCache->{no_expire}
);
}
}
@@ -91,15 +93,17 @@
return $c->next::method(@_)
unless $c->req->method =~ m/^(?:GET|HEAD)$/;
+ my $config_PageCache = $c->config->{'Plugin::PageCache'};
+
my $hook =
- $c->config->{'Plugin::PageCache'}->{cache_hook}
- ? $c->can($c->config->{'Plugin::PageCache'}->{cache_hook})
+ $config_PageCache->{cache_hook}
+ ? $c->can($config_PageCache->{cache_hook})
: undef;
return $c->next::method(@_) if ( $hook && !$c->$hook() );
return $c->next::method(@_)
- if ( $c->config->{'Plugin::PageCache'}->{auto_check_user}
+ if ( $config_PageCache->{auto_check_user}
&& $c->can('user_exists')
&& $c->user_exists);
@@ -115,7 +119,7 @@
# Time to remove page from cache?
if ( $data->{expire_time} && $data->{expire_time} <= time ) {
- if ( my $busy_lock = $c->config->{'Plugin::PageCache'}->{busy_lock} ) {
+ if ( my $busy_lock = $config_PageCache->{busy_lock} ) {
# Extend the expiration time for others while
# this caller refreshes the cache
$data->{expire_time} = time() + $busy_lock;
@@ -123,19 +127,19 @@
$cache->set( $key, $data );
$c->log->debug( "$key has expired, being refreshed for $busy_lock seconds" )
- if ($c->config->{'Plugin::PageCache'}->{debug});
+ if ($config_PageCache->{debug});
}
else {
$c->log->debug( "Expiring $key from page cache" )
- if ($c->config->{'Plugin::PageCache'}->{debug});
+ if ($config_PageCache->{debug});
$cache->remove( $key );
- if ( !$c->config->{'Plugin::PageCache'}->{disable_index} ) {
+ if ( !$config_PageCache->{disable_index} ) {
my $index = $cache->get( "_page_cache_index" ) || {};
delete $index->{$key};
$cache->set( "_page_cache_index", $index,
- $c->config->{'Plugin::PageCache'}->{no_expire});
+ $config_PageCache->{no_expire});
}
}
@@ -145,7 +149,7 @@
$c->log->debug("Serving $key from page cache, expires in "
. ($data->{expire_time} - time)
. " seconds")
- if ($c->config->{'Plugin::PageCache'}->{debug});
+ if ($config_PageCache->{debug});
$c->_page_cache_used( 1 );
@@ -192,41 +196,43 @@
sub _set_page_cache_headers {
my ( $c, $data ) = @_;
+ my $headers = $c->res->headers;
+ my $config_PageCache = $c->config->{'Plugin::PageCache'};
- if ( $c->config->{'Plugin::PageCache'}->{cache_headers} ) {
+ if ( $config_PageCache->{cache_headers} ) {
for my $header_key ( keys %{ $data->{headers} || {} } ) {
- $c->res->headers->header(
+ $headers->header(
$header_key => $data->{headers}->{$header_key}
);
}
}
- return unless $c->config->{'Plugin::PageCache'}->{set_http_headers};
+ return unless $config_PageCache->{set_http_headers};
if ( exists $data->{expires} ) {
# page cache but not client cache
if ( !$data->{expires} ) {
- $c->res->headers->header( 'Cache-Control' => 'no-cache' );
- $c->res->headers->header( 'Pragma' => 'no-cache' );
+ $headers->header( 'Cache-Control' => 'no-cache' );
+ $headers->header( 'Pragma' => 'no-cache' );
return;
}
- $c->res->headers->header(
+ $headers->header(
'Cache-Control' => "max-age=" . $data->{expires});
- $c->res->headers->expires( time + $data->{expires} );
+ $headers->expires( time + $data->{expires} );
}
else {
- $c->res->headers->header(
+ $headers->header(
'Cache-Control' => "max-age=" . ($data->{expire_time} - time));
- $c->res->headers->expires( $data->{expire_time} );
+ $headers->expires( $data->{expire_time} );
}
- $c->res->headers->last_modified( $data->{create_time} )
+ $headers->last_modified( $data->{create_time} )
unless $c->res->status && $c->res->status == 304;
}
@@ -236,14 +242,16 @@
# never cache POST requests
return $c->next::method(@_) if ( $c->req->method eq "POST" );
+ my $config_PageCache = $c->config->{'Plugin::PageCache'};
+
my $hook =
- $c->config->{'Plugin::PageCache'}->{cache_hook}
- ? $c->can($c->config->{'Plugin::PageCache'}->{cache_hook})
+ $config_PageCache->{cache_hook}
+ ? $c->can($config_PageCache->{cache_hook})
: undef;
return $c->next::method(@_) if ( $hook && !$c->$hook() );
return $c->next::method(@_)
- if ( $c->config->{'Plugin::PageCache'}->{auto_check_user}
+ if ( $config_PageCache->{auto_check_user}
&& $c->can('user_exists')
&& $c->user_exists);
return $c->next::method(@_) if ( scalar @{ $c->error } );
@@ -253,7 +261,7 @@
return $c->next::method(@_) if ( $c->_page_cache_used );
if (!$c->_cache_page
- && scalar @{ $c->config->{'Plugin::PageCache'}->{auto_cache} })
+ && scalar @{ $config_PageCache->{auto_cache} })
{
# is this page part of the auto_cache list?
@@ -261,12 +269,12 @@
# For performance, this should be moved to setup, and generate a hash.
AUTO_CACHE:
- foreach my $auto (@{ $c->config->{'Plugin::PageCache'}->{auto_cache} })
+ foreach my $auto (@{ $config_PageCache->{auto_cache} })
{
next if $auto =~ m/^\d$/;
if ( $path =~ /^$auto$/ ) {
$c->log->debug( "Auto-caching page $path" )
- if ($c->config->{'Plugin::PageCache'}->{debug});
+ if ($config_PageCache->{debug});
$c->cache_page;
last AUTO_CACHE;
}
@@ -277,10 +285,11 @@
my $key = $c->_get_page_cache_key;
my $now = time;
+ my $headers = $c->res->headers;
$c->log->debug(
"Caching page $key for $options->{cache_seconds} seconds"
- ) if ($c->config->{'Plugin::PageCache'}->{debug});
+ ) if ($config_PageCache->{debug});
# Cache some additional metadata along with the content
# Some caches don't support expirations, so we do it manually
@@ -289,15 +298,15 @@
content_type => [ $c->res->content_type ] || undef,
content_encoding => $c->res->content_encoding || undef,
create_time => $options->{last_modified}
- || $c->res->headers->last_modified
+ || $headers->last_modified
|| $now,
expire_time => $now + $options->{cache_seconds},
};
- if ( $c->config->{'Plugin::PageCache'}->{cache_headers} ) {
+ if ( $config_PageCache->{cache_headers} ) {
$data->{headers} = {
- map { $_ => $c->res->headers->header($_) }
- $c->res->headers->header_field_names
+ map { $_ => $headers->header($_) }
+ $headers->header_field_names
};
}
@@ -309,7 +318,7 @@
$c->_set_page_cache_headers( $data ); # don't forget the first time
- if ( !$c->config->{'Plugin::PageCache'}->{disable_index} ) {
+ if ( !$config_PageCache->{disable_index} ) {
# Keep an index cache of all pages that have been cached, for use
# with clear_cached_page
my $index = $cache->get( "_page_cache_index" ) || {};
@@ -317,7 +326,7 @@
# Save date in cache
$cache->set( "_page_cache_index", $index,
- $c->config->{'Plugin::PageCache'}->{no_expire});
+ $config_PageCache->{no_expire});
}
# Check for If-Modified-Since
@@ -337,14 +346,16 @@
$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'}->{cache_headers} ||= 0;
- $c->config->{'Plugin::PageCache'}->{set_http_headers} ||= 0;
- $c->config->{'Plugin::PageCache'}->{disable_index} ||= 0;
- $c->config->{'Plugin::PageCache'}->{busy_lock} ||= 0;
- $c->config->{'Plugin::PageCache'}->{debug} ||= $c->debug;
+ my $config_PageCache = $c->config->{'Plugin::PageCache'} ||= {};
+ $config_PageCache->{auto_cache} ||= [];
+ $config_PageCache->{expires} ||= 60 * 5;
+ $config_PageCache->{cache_headers} ||= 0;
+ $config_PageCache->{set_http_headers} ||= 0;
+ $config_PageCache->{disable_index} ||= 0;
+ $config_PageCache->{busy_lock} ||= 0;
+ $config_PageCache->{debug} ||= $c->debug;
+
# detect the cache plugin being used and set appropriate
# never-expires syntax
if ( $c->can('cache') ) {
@@ -358,7 +369,7 @@
# Older Cache plugins
if ( $cache->isa('Cache::FileCache') ) {
- $c->config->{'Plugin::PageCache'}->{no_expire} = "never";
+ $config_PageCache->{no_expire} = "never";
}
elsif ($cache->isa('Cache::Memcached')
|| $cache->isa('Cache::FastMmap'))
@@ -366,7 +377,7 @@
# Memcached defaults to 'never' when not given an expiration
# In FastMmap, it's not possible to set an expiration
- $c->config->{'Plugin::PageCache'}->{no_expire} = undef;
+ $config_PageCache->{no_expire} = undef;
}
}
else {
More information about the Catalyst-commits
mailing list