diff options
author | Adrian Lang <mail@adrianlang.de> | 2011-07-01 16:49:28 +0200 |
---|---|---|
committer | Adrian Lang <mail@adrianlang.de> | 2011-07-02 23:10:26 +0200 |
commit | 6619f42eb475ad91a0b73d1aa6268ff41c6129a2 (patch) | |
tree | 44342d0ad8f7e56eca755e37b01204c339effb20 | |
parent | 29dc4f32ec672f86f16846b70a777990822d184c (diff) | |
download | dokuwiki-6619f42eb475ad91a0b73d1aa6268ff41c6129a2.tar.gz dokuwiki-6619f42eb475ad91a0b73d1aa6268ff41c6129a2.zip |
Refactor CSS and JS caching
* Increase HTTP cache time since the resources are timestamped on request
anyway
* Check userscript.js only once for JS cache validation
* Use cache class
-rw-r--r-- | inc/httputils.php | 52 | ||||
-rw-r--r-- | lib/exe/css.php | 68 | ||||
-rw-r--r-- | lib/exe/js.php | 67 |
3 files changed, 70 insertions, 117 deletions
diff --git a/inc/httputils.php b/inc/httputils.php index 8da42e3b7..0ad97a9a1 100644 --- a/inc/httputils.php +++ b/inc/httputils.php @@ -197,3 +197,55 @@ function http_gzip_valid($uncompressed_file) { return true; } + +/** + * Set HTTP headers and echo cachefile, if useable + * + * This function handles output of cacheable resource files. It ses the needed + * HTTP headers. If a useable cache is present, it is passed to the web server + * and the scrpt is terminated. + */ +function http_cached($cache, $cache_ok) { + global $conf; + + // check cache age & handle conditional request + // since the resource files are timestamped, we can use a long max age: 1 year + header('Cache-Control: public, max-age=31536000'); + header('Pragma: public'); + if($cache_ok){ + http_conditionalRequest(filemtime($cache)); + if($conf['allowdebug']) header("X-CacheUsed: $cache"); + + // finally send output + if ($conf['gzip_output'] && http_gzip_valid($cache)) { + header('Vary: Accept-Encoding'); + header('Content-Encoding: gzip'); + readfile($cache.".gz"); + } else { + if (!http_sendfile($cache)) readfile($cache); + } + exit; + } + + http_conditionalRequest(time()); +} + +/** + * Cache content and print it + */ +function http_cached_finish($file, $content) { + global $conf; + + // save cache file + io_saveFile($file, $content); + if(function_exists('gzopen')) io_saveFile("$file.gz",$content); + + // finally send output + if ($conf['gzip_output']) { + header('Vary: Accept-Encoding'); + header('Content-Encoding: gzip'); + print gzencode($content,9,FORCE_GZIP); + } else { + print $content; + } +} diff --git a/lib/exe/css.php b/lib/exe/css.php index e4105b427..9751b5be4 100644 --- a/lib/exe/css.php +++ b/lib/exe/css.php @@ -46,7 +46,7 @@ function css_out(){ } // The generated script depends on some dynamic options - $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css'); + $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css'); // load template styles $tplstyles = array(); @@ -87,26 +87,14 @@ function css_out(){ } } - // check cache age & handle conditional request - header('Cache-Control: public, max-age=3600'); - header('Pragma: public'); - if(css_cacheok($cache,array_keys($files),$tplinc)){ - http_conditionalRequest(filemtime($cache)); - if($conf['allowdebug']) header("X-CacheUsed: $cache"); - - // finally send output - if ($conf['gzip_output'] && http_gzip_valid($cache)) { - header('Vary: Accept-Encoding'); - header('Content-Encoding: gzip'); - readfile($cache.".gz"); - } else { - if (!http_sendfile($cache)) readfile($cache); - } + $cache_files = array_merge(array_keys($files), getConfigFiles('main')); + $cache_files[] = $tplinc.'style.ini'; + $cache_files[] = __FILE__; - return; - } else { - http_conditionalRequest(time()); - } + // check cache age & handle conditional request + // This may exit if a cache can be used + http_cached($cache->cache, + $cache->useCache(array('files' => $cache_files))); // start output buffering and build the stylesheet ob_start(); @@ -135,45 +123,7 @@ function css_out(){ $css = css_compress($css); } - // save cache file - io_saveFile($cache,$css); - if(function_exists('gzopen')) io_saveFile("$cache.gz",$css); - - // finally send output - if ($conf['gzip_output']) { - header('Vary: Accept-Encoding'); - header('Content-Encoding: gzip'); - print gzencode($css,9,FORCE_GZIP); - } else { - print $css; - } -} - -/** - * Checks if a CSS Cache file still is valid - * - * @author Andreas Gohr <andi@splitbrain.org> - */ -function css_cacheok($cache,$files,$tplinc){ - global $config_cascade; - - if(isset($_REQUEST['purge'])) return false; //support purge request - - $ctime = @filemtime($cache); - if(!$ctime) return false; //There is no cache - - // some additional files to check - $files = array_merge($files, getConfigFiles('main')); - $files[] = $tplinc.'style.ini'; - $files[] = __FILE__; - - // now walk the files - foreach($files as $file){ - if(@filemtime($file) > $ctime){ - return false; - } - } - return true; + http_cached_finish($cache->cache, $css); } /** diff --git a/lib/exe/js.php b/lib/exe/js.php index 28894ffc6..0688825c6 100644 --- a/lib/exe/js.php +++ b/lib/exe/js.php @@ -32,7 +32,8 @@ function js_out(){ global $config_cascade; // The generated script depends on some dynamic options - $cache = getCacheName('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.js'); + $cache = new cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'], + '.js'); // load minified version for some files $min = $conf['compress'] ? '.min' : ''; @@ -70,25 +71,13 @@ function js_out(){ $files[] = $config_cascade['userscript']['default']; } + $cache_files = array_merge($files, getConfigFiles('main')); + $cache_files[] = __FILE__; + // check cache age & handle conditional request - header('Cache-Control: public, max-age=3600'); - header('Pragma: public'); - if(js_cacheok($cache,$files)){ - http_conditionalRequest(filemtime($cache)); - if($conf['allowdebug']) header("X-CacheUsed: $cache"); - - // finally send output - if ($conf['gzip_output'] && http_gzip_valid($cache)) { - header('Vary: Accept-Encoding'); - header('Content-Encoding: gzip'); - readfile($cache.".gz"); - } else { - if (!http_sendfile($cache)) readfile($cache); - } - return; - } else { - http_conditionalRequest(time()); - } + // This may exit if a cache can be used + http_cached($cache->cache, + $cache->useCache(array('files' => $cache_files))); // start output buffering and build the script ob_start(); @@ -137,18 +126,7 @@ function js_out(){ $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 - // save cache file - io_saveFile($cache,$js); - if(function_exists('gzopen')) io_saveFile("$cache.gz",$js); - - // finally send output - if ($conf['gzip_output']) { - header('Vary: Accept-Encoding'); - header('Content-Encoding: gzip'); - print gzencode($js,9,FORCE_GZIP); - } else { - print $js; - } + http_cached_finish($cache->cache, $js); } /** @@ -184,33 +162,6 @@ function js_load($file){ } /** - * Checks if a JavaScript Cache file still is valid - * - * @author Andreas Gohr <andi@splitbrain.org> - */ -function js_cacheok($cache,$files){ - if(isset($_REQUEST['purge'])) return false; //support purge request - - $ctime = @filemtime($cache); - if(!$ctime) return false; //There is no cache - - global $config_cascade; - - // some additional files to check - $files = array_merge($files, getConfigFiles('main')); - $files[] = $config_cascade['userscript']['default']; - $files[] = __FILE__; - - // now walk the files - foreach($files as $file){ - if(@filemtime($file) > $ctime){ - return false; - } - } - return true; -} - -/** * Returns a list of possible Plugin Scripts (no existance check here) * * @author Andreas Gohr <andi@splitbrain.org> |