[Catalyst-commits] r13669 - trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin

timbunce at dev.catalyst.perl.org timbunce at dev.catalyst.perl.org
Mon Nov 1 12:01:55 GMT 2010


Author: timbunce
Date: 2010-11-01 12:01:55 +0000 (Mon, 01 Nov 2010)
New Revision: 13669

Modified:
   trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
Log:
Make fewer calls to $c->cache as it can be expensive

Modified: trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm
===================================================================
--- trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm	2010-11-01 11:52:40 UTC (rev 13668)
+++ trunk/Catalyst-Plugin-PageCache/lib/Catalyst/Plugin/PageCache.pm	2010-11-01 12:01:55 UTC (rev 13669)
@@ -60,11 +60,13 @@
 
     my $removed = 0;
     
-    my $index = $c->cache->get( "_page_cache_index" ) || {};
+    my $cache = $c->cache; # curry the cache just once, here
 
+    my $index = $cache->get( "_page_cache_index" ) || {};
+
     foreach my $key ( keys %{$index} ) {
         if ( $key =~ /^(?::[^:]+:)?$uri$/xms ) {
-            $c->cache->remove( $key );
+            $cache->remove( $key );
             delete $index->{$key};
             $removed++;
         
@@ -73,7 +75,7 @@
     }
 
     if ( $removed ) {
-        $c->cache->set(
+        $cache->set(
             "_page_cache_index",
             $index,
             $c->config->{'Plugin::PageCache'}->{no_expire}
@@ -105,8 +107,10 @@
     return $c->next::method(@_)
         unless my $key = $c->_get_page_cache_key;
 
+    my $cache = $c->cache; # curry the cache just once, here
+
     return $c->next::method(@_)
-        unless my $data = $c->cache->get( $key );
+        unless my $data = $cache->get( $key );
 
     # Time to remove page from cache?
 
@@ -116,7 +120,7 @@
             # this caller refreshes the cache
             $data->{expire_time} = time() + $busy_lock;
             
-            $c->cache->set( $key, $data );
+            $cache->set( $key, $data );
             
             $c->log->debug( "$key has expired, being refreshed for $busy_lock seconds" )
                 if ($c->config->{'Plugin::PageCache'}->{debug});
@@ -125,12 +129,12 @@
             $c->log->debug( "Expiring $key from page cache" )
               if ($c->config->{'Plugin::PageCache'}->{debug});
 
-            $c->cache->remove( $key );
+            $cache->remove( $key );
 
             if ( !$c->config->{'Plugin::PageCache'}->{disable_index} ) {
-                my $index = $c->cache->get( "_page_cache_index" ) || {};
+                my $index = $cache->get( "_page_cache_index" ) || {};
                 delete $index->{$key};
-                $c->cache->set( "_page_cache_index", $index,
+                $cache->set( "_page_cache_index", $index,
                     $c->config->{'Plugin::PageCache'}->{no_expire});
             }
         }
@@ -299,18 +303,20 @@
 
         $data->{expires} = $options->{expires} if exists $options->{expires};
 
-        $c->cache->set( $key, $data );
+        my $cache = $c->cache; # curry the cache just once, here
 
+        $cache->set( $key, $data );
+
         $c->_set_page_cache_headers( $data );  # don't forget the first time
 
         if ( !$c->config->{'Plugin::PageCache'}->{disable_index} ) {
             # Keep an index cache of all pages that have been cached, for use
             # with clear_cached_page
-            my $index = $c->cache->get( "_page_cache_index" ) || {};
+            my $index = $cache->get( "_page_cache_index" ) || {};
             $index->{$key} = 1;
 
             # Save date in cache
-            $c->cache->set( "_page_cache_index", $index,
+            $cache->set( "_page_cache_index", $index,
                 $c->config->{'Plugin::PageCache'}->{no_expire});
         }
 
@@ -342,18 +348,20 @@
     # detect the cache plugin being used and set appropriate
     # never-expires syntax
     if ( $c->can('cache') ) {
-        
+
         # Newer C::P::Cache, cannot call $c->cache as a package method
         if ( $c->isa('Catalyst::Plugin::Cache') ) {
             return;
         }
 
+        my $cache = $c->cache; # curry the cache just once, here
+
         # Older Cache plugins
-        if ( $c->cache->isa('Cache::FileCache') ) {
+        if ( $cache->isa('Cache::FileCache') ) {
             $c->config->{'Plugin::PageCache'}->{no_expire} = "never";
         }
-        elsif ($c->cache->isa('Cache::Memcached')
-            || $c->cache->isa('Cache::FastMmap'))
+        elsif ($cache->isa('Cache::Memcached')
+            || $cache->isa('Cache::FastMmap'))
         {
 
           # Memcached defaults to 'never' when not given an expiration




More information about the Catalyst-commits mailing list