Welcome to the Cookbook

loading...
Please login to continue

8.7.2 Cache::write()

Cache::write($key, $value, $config = null);

Cache::write() はキャッシュに $value を書き込みます。$key を参照することで、この値を読み込んだり削除したりすることができます。また、キャッシュを格納するコンフィグレーションを特定することもできます。$config が指定されなかった場合、デフォルトが使用されます。Cache::write() にはあらゆる型のオブジェクトを格納することができ、モデルのデータを完全に格納することもできます。

	if (($posts = Cache::read('posts')) === false) {
		$posts = $this->Post->find('all');
		Cache::write('posts', $posts);
	}
  1. if (($posts = Cache::read('posts')) === false) {
  2. $posts = $this->Post->find('all');
  3. Cache::write('posts', $posts);
  4. }

Cache::write() と Cache::read() を利用することで、モデルがデータベースから posts を取得する回数を、容易に削減することができます。