Cache control
Manipulating the HTTP cache is a great way to control the impact of visitors and users on your application's resources. CakePHP provides a range of options to control both expiration and validation, and manage how clients and proxies engage with your content.
In this recipe, we'll look at the various ways you can handle the HTTP cache to your advantage.
Getting ready
In this recipe, we'll use a simple controller to show each of the caching options available. So, create a file named CacheController.php
in app/Controller/
, with the following content:
<?php App::uses('AppController', 'Controller'); class CacheController extends AppController { public $autoRender = false; }
How to do it...
Perform the following steps:
Add a
cached()
method to theCacheController
class, as shown in the following code:public function cached() { $this->response->cache('-1 hour', '+1 day'); return __('I am cached!'); }
In the same class, also add an
expire()
method:public function expire(...