There are a few ways to cache content in CakePHP. There is a feature to cache the output but I’ve found that a little tricky to get right in the past. When the cache expired the action wasn’t called so we had a page with no content… not quite the effect I was looking for.
You can always, however, cache anything you want within your actions using the built-in Cache library. The code to do this is:
if (($data = Cache::read('key')) === false) {
$data = $this->Model->getMyData();
Cache::write('key', $data);
}
Obviously, you need to set your own variable names, cache key and make a method to get your data. The good thing about this method is that if you need the same data across a few pages you can use the same key in each place.
Or, to make it even simpler, you could move the caching into your model, then you don’t need to worry about it in your controllers.


Comments
There are no responses to “Caching “things” in CakePHP” (Subscribe by RSS)
Comments are closed.