I have a question about the way view caching and browser caching work together in CakePHP 2.1.

I've just upgraded my app to CakePHP 2.1, and set up HTTP caching using the new $this->response->modified method (which works well):

class ArticlesController extends AppController {

public function view($id) {

$article = $this->Article->find(

'first',

array('conditions' => array('Article.id' => $id))

);

$this->response->modified($article['Article']['modified']);

$this->set(compact('article'));

}

}

I have also set up CakePHP view caching:

class ArticlesController extends AppController {

public $cacheAction = array(

'view' => array('callbacks' => true, 'duration' => "1 week"),

}

}

Both work well when used independently. However, when both are enabled, the CakePHP view caching seems to override the browser caching (specifically, no Last-Modified header is sent when pages are served from the CakePHP view cache). This stops the browser from caching pages that are being served from CakePHP's view cache.

Ideally, I would like the browser to cache pages even if they are being served from CakePHP's cache (i.e. I would like CakePHP to return a Last-Modified header, and respond to the browser's If-Modified-Since request header, regardless of whether CakePHP has is returning a cached copy of page or not).

I'm just wondering whether this is expected behavior, whether I'm doing something wrong, or whether it's something that has not been considered (or is not thought to be important).

网友答案:

View caching, by nature, doesn't actually execute the controller's method on every request. I'm guessing it executes the action once and then caches the output to disk (or, whatever cache engine you happen to be using). If you look at the CacheHelper's _writeFile method, you can see how the cached view is constructed.