home>>PHP FW Kohanaを使う>>アプリケーションを作る

アプリケーションを作る

Cacheを使う

Cacheクラスを使えるようにする。これで長々書いてきたアプリ作成覚えは終わる(多分)。

ぼくの使い方としては、KohanaドキュメントのCacheのいちばん簡単な部分を真似すれば十分で、config/cache.phpでFileキャッシュの設定をして(<=デフォルト)、コントローラで頁全体のキャッシュを送出するだけ[1]

コントローラーの表示メソッドの当該部

(略)
$cache = Cache::instance();
$cache_id = $this->model->get('fullpath');<=一意のキャッシュID
$exists = $cache -> get($cache_id);     ファイルのフルパス  

if(!$exists || $nocache) {
    $content = $this->model->get_content();
    $parser = new TagParser($this->model);
    $parser->set_content($content);
    $template = new View(site::$info['skin'].$this->view_name);
    $template->content = $parser->parse();
    $template->parser = $parser;
    $template->dirs = $this->model->get_yml_info(
                       $this->model->pathinfo['filepath'],'dirs'
                       );
    $render = $template->render();<=変数に格納
    print $render;

    if($nocache) {
        $cache->delete($cache_id);
    } else {
        $cache->set($cache_id,$render);
    }

    exit();
} else {
    print $exists;    
    exit();
}
(略)

当然だが描写速度は劇的に早くなった。














annex

補足情報はありません













note
[1] 頁を書いていて、キャッシュなしに表示させたいとき、 キャッシュを消したい時は、URLにクエリー・ストリング(?秘密の文字)を付け加えると 本文の引用コード中の$nocache変数がTRUEになる。