Changeset 264 in t29-www


Ignore:
Timestamp:
May 31, 2012, 1:47:32 AM (7 years ago)
Author:
sven
Message:

Saubere Caching-Klasse eingefuehrt, um Code-Redundanzen in technikum29.php und js.php zu vermeiden und mehr/einheitliche Caching-Funktionalitaet bereitzustellen. Das Caching kann jetzt debuggt werden und so. Und ist prima dokumentiert. Ausserdem kann der Caching-Ordner jetzt einfach gewechselt werden.

Location:
lib
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • lib/js.php

    r262 r264  
    44$lib = dirname(__FILE__);
    55$webroot = realpath("$lib/../");  # file path to root of t29 web installation
    6 $cache_file = "$webroot/shared/cache/compressed.js";
     6$cache_file = 'compressed.js';
    77$module_dir = "$webroot/shared/js-v6/modules";
    88$modules = glob("$module_dir/*.js");
    99
    1010$debug = isset($_GET['debug']); // skip cache and just concat everything
    11 $purge_cache = isset($_GET['purge_cache']); // rebuild cache file (compressed)
    1211
    13 if(!$debug && !$purge_cache) {
    14         // check cache and all input files
    15         $filem_cache = @filemtime($cache_file);
    16         $filem_moddir = @filemtime($module_dir); // if new modules were added
    17         $filem_modules = array_reduce(array_map(function($x){return @filemtime($x);}, $modules), 'max');
    18         $cache_valid = $filem_cache && $filem_modules < $filem_cache && $filem_moddir < $filem_cache;
    19 
    20         if($cache_valid) {
    21                 header("Last-Modified: ".gmdate("D, d M Y H:i:s", $filem_cache)." GMT");
    22                 if(@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $filem_cache)
    23                         // client already has page cached locally
    24                         header("HTTP/1.1 304 Not Modified");
    25                 else
    26                         readfile($cache_file);
    27                 exit;
    28         }
     12if(!$debug) {
     13        require "$lib/cache.php";
     14        $js_cache = new t29Cache();
     15        $js_cache->test_files = $modules;
     16        $js_cache->set_cache_file($webroot, $cache_file);
     17        $js_cache->try_cache_and_exit();
     18        $js_cache->start_cache('minify_javascript', true);
    2919}
    3020
    31 if(!$debug) ob_start();
    3221$filenames = array_map('basename', $modules); // filenames like foo.js
    3322?>
     
    4736 */
    4837<?php
     38
     39$header = ob_get_contents(); // for prepending it to minified code
    4940
    5041foreach($modules as $i => $mod) {
     
    6657}
    6758
    68 if($debug) exit; // just here without compressing etc.
    6959
    70 $code = ob_get_flush();
    71 
    72 // reduces code size about 1/2 - before: 23kB, after: 12kB
    73 require "$lib/JavaScriptMinifier.php";
    74 $code = JavaScriptMinifier::minify($code);
    75 
    76 // write out file.
    77 file_put_contents($cache_file, $code);
    78 
    79 print $code;
     60function minify_javascript($code) {
     61        global $lib, $header;
     62        // reduces code size about 1/2 - before: 23kB, after: 12kB
     63        require "$lib/JavaScriptMinifier.php";
     64        $minified = JavaScriptMinifier::minify($code);
     65        return $header . $minified;
     66}
  • lib/technikum29.php

    r262 r264  
    2626$lang_path = $languages[$lang][1]; # shorthand, relative to webroot. use "$webroot$lang_path" for local.
    2727
    28 # Calling parameters
    29 $skip_cache = isset($_GET['skip_cache']);
    30 $purge_cache = isset($_GET['purge_cache']);
     28require "$lib/cache.php";
    3129
    32 # lightweight caching system
    33 $test_programs = array(
     30$page_cache = new t29Cache(false, true); // debug, verbose
     31$page_cache->set_cache_file($webroot, $file);
     32$page_cache->test_files = array(
    3433        __FILE__,
    3534        $_SERVER['SCRIPT_FILENAME'],
     
    4140);
    4241
    43 $cache_file = $cache_dir . $file;
    44 $last_cache = @filemtime($cache_dir.$file);
    45 $last_program = array_reduce(array_map(function($x){return @filemtime($x);}, $test_programs), 'max');
    46 $cache_valid = $last_cache && $last_program < $last_cache;
     42$page_cache->try_cache_and_exit();
    4743
    48 if(!$cache_valid || $skip_cache || $purge_cache) {
    49         // rebuild cache
    50         require "$lib/template.php";
    51         $tmpl = new t29Template($GLOBALS);
    52         $tmpl->create_cache();
    53 } else {
    54         // use cache
    55         header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_cache)." GMT");
    56         //header("Etag: $etag");
    57 
    58         if(@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_cache) {
    59                 // client already has page cached locally
    60                 header("HTTP/1.1 304 Not Modified");
    61         } else {
    62                 readfile($cache_file);
    63         }
    64         exit;
    65 }
     44// cache missed, rebuild cache
     45require "$lib/template.php";
     46$tmpl = new t29Template($GLOBALS);
     47$tmpl->create_cache($page_cache);
    6648
    6749// end of technikum29.php
  • lib/template.php

    r262 r264  
    5757         *                   (does not change behaviour of this file's code)
    5858         **/
    59         function create_cache() {
    60                 ob_start();
     59        function create_cache($cache_object) {
     60                $cache_object->start_cache(array($this, 'print_footer'));
    6161                $this->print_header();
    62                 register_shutdown_function(array($this, 'create_cache_shutdown'));
    63         }
    64        
    65         function create_cache_shutdown() {
    66                 $this->print_footer();
    67                 $whole_page = ob_get_flush();
    68                 if($this->conf['skip_cache']) {
    69                         echo "<!-- debug mode, skipped cache and cache saving. -->";
    70                         return; // do not save anything
    71                 }
    72                
    73                 if(!file_exists($this->conf['cache_file'])) {
    74                         if(!self::mkdir_recursive(dirname($this->conf['cache_file']))) {
    75                                 print "<div class='error'>Could not create recursive caching directories</div>";
    76                         }
    77                 }
    78                
    79                 if(@file_put_contents($this->conf['cache_file'], $whole_page))
    80                         print "<!-- Wrote output cache successfully -->\n";
    81                 else
    82                         print "<div class='error'>Could not write page output cache to ".$this->conf['cache_file']."</div>";
    83         }
    84 
    85         public static function mkdir_recursive($pathname) {
    86                 is_dir(dirname($pathname)) || self::mkdir_recursive(dirname($pathname));
    87                 return is_dir($pathname) || @mkdir($pathname);
    8862        }
    8963
     
    9973  <meta name="description" content="Produziert am 08.01.2012">
    10074  <meta name="author" content="Sven">
    101   <meta name="generator" content="t29v6 $Id$">
     75  <meta name="generator" content="t29v6">
    10276  <meta name="t29.cachedate" content="<?php print date('r'); ?>">
     77  <?php
     78        if(isset($this->conf['version'])) printf('<meta name="t29.version" content="%s">', $this->conf['version']);
     79  ?>
    10380 
    10481  <?php
Note: See TracChangeset for help on using the changeset viewer.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License