CakePHP 2.1: Browser cache vs View cache
Asked Answered
T

1

9

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).

Tewell answered 16/3, 2012 at 14:58 Comment(0)
K
2

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.

    $file = '<!--cachetime:' . $cacheTime . '--><?php';

    if (empty($this->_View->plugin)) {
        $file .= "
        App::uses('{$this->_View->name}Controller', 'Controller');
        ";
    } else {
        $file .= "
        App::uses('{$this->_View->plugin}AppController', '{$this->_View->plugin}.Controller');
        App::uses('{$this->_View->name}Controller', '{$this->_View->plugin}.Controller');
        ";
    }

    $file .= '
            $request = unserialize(\'' . str_replace("'", "\\'", serialize($this->request)) . '\');
            $response = new CakeResponse(array("charset" => Configure::read("App.encoding")));
            $controller = new ' . $this->_View->name . 'Controller($request, $response);
            $controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
            $controller->helpers = $this->helpers = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->helpers)) . '\'));
            $controller->layout = $this->layout = \'' . $this->_View->layout . '\';
            $controller->theme = $this->theme = \'' . $this->_View->theme . '\';
            $controller->viewVars = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->viewVars)) . '\'));
            Router::setRequestInfo($controller->request);
            $this->request = $request;';

    if ($useCallbacks == true) {
        $file .= '
            $controller->constructClasses();
            $controller->startupProcess();';
    }

    $file .= '
            $this->viewVars = $controller->viewVars;
            $this->loadHelpers();
            extract($this->viewVars, EXTR_SKIP);
    ?>';

It creates a new Controller object (with a new CakeResponse) and loads all the helpers, plugins, etc that may be used in the view and writes it to cache.

It appears that adding the Last-Modified header to the response of a cache action/view might require some deep modifications to the core CakePHP library.

Keil answered 10/5, 2012 at 4:12 Comment(1)
Yes, any modification to the Response object made by a Controller action will not be cached, this includes the setting of Cache control headers. There is an open ticket about this (although not Caching related): cakephp.lighthouseapp.com/projects/42648/tickets/… The only suggested workaround is setting headers manually in either the layout or Controller::beforeFilter() (with callback enabled).Deserving

© 2022 - 2024 — McMap. All rights reserved.