Show
Ignore:
Timestamp:
09/12/08 19:58:18 (4 months ago)
Author:
MattRead
Message:

plugin:staticcache NOTE: This update changes the basic structure of the
cache store. Please delete the cache or you will probably get errors.
There should a button to invalidate staticcaches cache, but there isn't
yet. If you want, please add one :).

Changing cache structure to include the caching of
headers to help prevent improper caching on redirects. Unfortunately we
still cannot grab the status header, so it will send 200, or 302 for
redirects. Also adding 404 detection, via the "matched rule", to ensure
that 404 response will not be cached and served as 200.

There is also now some basic hits/misses stats to show the percentage of
requests that are served cached pages (hits), and percentage that are
not (misses).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/staticcache/trunk/staticcache.plugin.php

    r672 r922  
    4646            if ( isset( $cache[$query_id] ) ) { 
    4747                global $profile_start; 
    48                 echo $cache[$query_id]; 
     48                 
     49                foreach( $cache[$query_id]['headers'] as $header ) { 
     50                    header($header); 
     51                } 
     52                echo $cache[$query_id]['body']; 
    4953                $time = microtime(true) - $profile_start; 
    5054                echo "<!-- Served by StaticCache in $time seconds -->"; 
     
    5357                    ( Options::get('staticcache__average_time') + $time ) / 2 
    5458                ); 
     59                Options::set('staticcache__hits', Options::get('staticcache__hits') + 1); 
    5560                exit; 
    5661            } 
    5762        } 
     63        Options::set('staticcache__misses', Options::get('staticcache__misses') + 1); 
    5864        ob_start( 'StaticCache_ob_end_flush' ); 
    5965    } 
     
    6874    public function filter_dash_module_static_cache( $module, $id, $theme ) 
    6975    { 
    70         $theme->static_cache_average = sprintf( '%0.4f', Options::get('staticcache__average_time') ); 
     76        $theme->static_cache_average = sprintf( '%.4f', Options::get('staticcache__average_time') ); 
    7177        $theme->static_cache_pages = count( Cache::get_group('staticcache') ); 
     78         
     79        $hits = Options::get('staticcache__hits'); 
     80        $misses = Options::get('staticcache__misses'); 
     81        $total = $hits + $misses; 
     82        $theme->static_cache_hits = sprintf('%.0f', $total > 0 ? ($hits/$total)*100 : 0); 
     83        $theme->static_cache_misses = sprintf('%.0f', $total > 0 ? ($misses/$total)*100 : 0); 
     84         
    7285        $module['content'] = $theme->fetch( 'static_cache_stats' ); 
    7386        return $module; 
     
    133146    } 
    134147     
     148    /** 
     149     * @todo add expire time option 
     150     * @todo add invalidate cache button 
     151     */ 
    135152    public function action_plugin_ui( $plugin_id, $action ) 
    136153    { 
     
    158175    public static function get_query_id() 
    159176    { 
    160         return crc32( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY ) ); 
     177        return crc32( parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY) ); 
    161178    } 
    162179     
     
    168185        } 
    169186        if ( ! $url ) { 
    170             $url = Site::get_url( 'host' ) . rtrim( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' ); 
     187            $url = Site::get_url( 'host' ) . rtrim( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/' ); 
    171188        } 
    172189        return crc32( $user_id . $url ); 
     
    176193function StaticCache_ob_end_flush( $buffer ) 
    177194{ 
     195    // prevent caching of 404 responses 
     196    if ( !URL::get_matched_rule() || URL::get_matched_rule()->name == 'display_404' ) { 
     197        return false; 
     198    } 
    178199    $request_id = StaticCache::get_request_id(); 
    179200    $query_id = StaticCache::get_query_id(); 
    180201     
    181     if ( Cache::has( array("staticcache", $request_id) ) ) { 
    182         $cache = Cache::get( array("staticcache", $request_id) ); 
    183         $cache[$query_id] = $buffer; 
    184     } 
    185     else { 
    186         $cache = array( $query_id => $buffer ); 
    187     } 
     202    $cache = array( $query_id => array( 
     203        'headers' => headers_list(), 
     204        'body' => $buffer 
     205        )); 
    188206    Cache::set( array("staticcache", $request_id), $cache ); 
    189207