「HTML convert time」をキャッシュ(index.html)に埋め込む方法です。
ちなみにパフォーマンスは、0.1~0.3秒近辺まで下がりました。当初目標の2秒を大幅に上回る成果です(^O^
実際にそこまで体感できないのは、別の要因と考えられます。
以下、説明が回りくどくなりそうなので、先にハック例を示します。
◆index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php if ( ( $_SERVER['REQUEST_URI'] == "/blog/" ) && ( $_SERVER['REMOTE_ADDR'] != "xxx.xxx.xxx.xxx" ) ) { $_timestart = ""; _timer_start(); mb_language('Japanese'); mb_http_input("auto"); mb_internal_encoding('utf-8'); mb_http_output('utf-8'); include( "index.html" ); exit; } $isHtmlCache = true; $phpCache = '[CACHE-MODE]<?php _timer_stop(1); ?>'; /* Short and sweet */ define('WP_USE_THEMES', true); require('./wp-blog-header.php'); function _timer_start() { global $_timestart; $mtime = explode(' ', microtime() ); $mtime = $mtime[1] + $mtime[0]; $_timestart = $mtime; return true; } function _timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal global $_timestart, $_timeend; $mtime = microtime(); $mtime = explode(' ',$mtime); $mtime = $mtime[1] + $mtime[0]; $_timeend = $mtime; $timetotal = $_timeend-$_timestart; $r = number_format($timetotal, $precision); if ( $display ) echo $r; return $r; } ?> |
◆wp-content/themes/[使用しているテーマ名]/footer.php
1 |
<?php echo get_num_queries(); ?> queries. --> HTML convert time: <?php global $phpCache; global $isHtmlCache; if ( $isHtmlCache == true ) { echo $phpCache; } else { timer_stop(1); } ?> seconds. |
第1のポイントは、wp-settings.phpで定義されている関数です。
timer_start()
timer_stop()
が、wp-settings.phpには、キャッシュ表示するにあたっては冗長な処理が多く含まれるため、余計なオーバーヘッドになります。
ので、別名で内部処理だけ流用します。
_timer_start()
_timer_stop()
※処理ではグローバル変数を使っていますので、これも変更しておきます。
次に、キャッシュ保存したindex.htmlをincludeする直前で、先ほどの_timer_start()を呼び出しておき、このincludeしている利点を利用して、index.htmlのソース内に
1 |
<?php _timer_stop(1); ?> |
が出力されるように仕組みます。
→ wp-content/themes/[使用しているテーマ名]/footer.php
あまりキレイにまとまっていないので、わかりづらいかも知れないですね。
No comments yet. You should be kind and add one!
By submitting a comment you grant typista a perpetual license to reproduce your words and name/web site in attribution. Inappropriate and irrelevant comments will be removed at an admin’s discretion. Your email is used for verification purposes only, it will never be shared.