Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.1: Browser cache vs View cache

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

like image 518
Tomba Avatar asked Mar 16 '12 14:03

Tomba


1 Answers

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.

like image 149
sesser Avatar answered Sep 23 '22 13:09

sesser