Friday 15 May 2015

html - Compress cached output using PHP output buffers -



html - Compress cached output using PHP output buffers -

by using 1 line code ob_start('ob_gzhandler'); @ top of page, php output 11 kb according chrome console. when tried cache output next code, found cached file saved 65kb. bigger output size trade off caching? there way compress cached output further? have tried adding htaccess rules html compression don't think helps.

$id = $_get["id"]; $cachefile ="cache/p_{$id}.html"; if (file_exists($cachefile)) { include($cachefile); echo "<!-- cached ".date('js f y h:i', filemtime($cachefile))." -->"; exit; } ob_start('ob_gzhandler'); $fp = fopen($cachefile, 'w'); // open cache file writing fwrite($fp, ob_get_contents()); // save contents of output buffer file fclose($fp); // close file ob_end_flush();

your cached file not gziped server, seek way:

class="lang-php prettyprint-override">ob_start('ob_gzhandler'); $id = $_get["id"]; $cachefile ="cache/p_{$id}.html"; if (file_exists($cachefile)) { include($cachefile); echo "<!-- cached ".date('js f y h:i', filemtime($cachefile))." -->"; } else { // html or else ... $fp = fopen($cachefile, 'w'); // open cache file writing fwrite($fp, ob_get_contents()); // save contents of output buffer file fclose($fp); // close file } ob_end_flush();

p.s. leave task of compressing web server (nginx, apache).

php html caching output-buffering ob-start

No comments:

Post a Comment