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 /inc | |
parent | 29dc4f32ec672f86f16846b70a777990822d184c (diff) | |
download | rpg-6619f42eb475ad91a0b73d1aa6268ff41c6129a2.tar.gz rpg-6619f42eb475ad91a0b73d1aa6268ff41c6129a2.tar.bz2 |
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
Diffstat (limited to 'inc')
-rw-r--r-- | inc/httputils.php | 52 |
1 files changed, 52 insertions, 0 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; + } +} |