From 6619f42eb475ad91a0b73d1aa6268ff41c6129a2 Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Fri, 1 Jul 2011 16:49:28 +0200 Subject: 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 --- inc/httputils.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'inc') 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; + } +} -- cgit v1.2.3