Home > Computer > Movable Type > Movable Type PHP化による負荷を軽減(HTTP 1.1 条件付きGET)

Movable Type PHP化による負荷を軽減(HTTP 1.1 条件付きGET)

MovableTypeをPHP化している場合には、
HTTP1.1で規定されている「条件付きGET」が行われないというエントリーを見つけた。

小粋空間: HTTP/1.1 の「条件付きGET」を利用して PHP ファイルアクセスによるサーバ負荷を削減する

詳細は小粋空間に書いてあるが、要するに、
HTMLファイルを.phpの拡張子にすると、
更新されていなくてもブラウザのキャッシュが使われず、
毎回ファイルがダウンロードされるということだ。

条件付きGETをするための方法は、
小粋空間にも書かれているこちらを参考にした。
「条件付きGET」のススメ - Ogawa::Memoranda


Jay's Roomの場合、特にファイルをインクルードしたりはしていないので、
単純なケースのコードを、テンプレートの先頭に書けばOK

<?php
$ts = getlastmod();

doConditionalGet($ts);

function doConditionalGet($timestamp) {
// A PHP implementation of conditional get, see
// http://fishbowl.pastiche.org/archives/001132.html
$last_modified = gmdate('D, d M Y H:i:s T', $timestamp);
$etag = '"'.md5($last_modified).'"';
// Send the headers
header("Last-Modified: $last_modified");
header("ETag: $etag");
// See if the client has provided the required headers
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
false;
if (!$if_modified_since && !$if_none_match) {
return;
}
// At least one of the headers is there - check them
if ($if_none_match && $if_none_match != $etag) {
return; // etag is there but doesn't match
}
if ($if_modified_since && $if_modified_since != $last_modified) {
return; // if-modified-since is there but doesn't match
}
// Nothing has changed since their last request - serve a 304 and exit
header('HTTP/1.1 304 Not Modified');
exit;
}
?>

これをメインページ・個別アーカイブ・カテゴリアーカイブ・日付アーカイブの先頭に記述して、
すべて再構築すれば完了。

これで表示が少し早くなればいいな。

Comments:0

コメントする

Trackbacks:0

Home > Computer > Movable Type > Movable Type PHP化による負荷を軽減(HTTP 1.1 条件付きGET)

Search

Feeds

Return to page top