1
votes

On a CakePHP site (2.3.1) I have noticed the cached view file size is very large (10-60MB) per page.

Typically with sites I would expect the caching to just store pure HTML output but Cake is adding serialised PHP at the top of the files. From a performance perspective this large file size is problematic, using up gigabytes of space (we have 1000s of pages), and is not suited to APC caching (default max file size 1MB).

This is an example block at the top of the cached view file:

    <!--cachetime:1363985272--><?php
    App::uses('StaticController', 'Controller');
            $request = unserialize(base64_decode('<removed>'));
            $response = new CakeResponse();
            $controller = new StaticController($request, $response);
            $controller->plugin = $this->plugin = '';
            $controller->helpers = $this->helpers = unserialize(base64_decode('<removed>'));
            $controller->layout = $this->layout = 'default';
            $controller->theme = $this->theme = '';
            $controller->viewVars = unserialize(base64_decode('<removed>'));
            Router::setRequestInfo($controller->request);
            $this->request = $request;
            $this->viewVars = $controller->viewVars;
            $this->loadHelpers();
            extract($this->viewVars, EXTR_SKIP);
    ?>

I'd prefer no PHP in there at all, as the HTML below is the static generated output. A massive overhead that accounts for all the file size.

Cache setting in bootstrap.php:

Cache::config('default', array('engine' => 'Apc'));

At present my only option is to improve the file size of the view cache files. Adding something like Varnish is not possible on this server as this point in time.

Any tips to resolve the file size issue would be great.

1
If you want static html files - why not write static html files =)? You might find this repo handy (it's pretty old, so expect to need to tweak the code. Concepts don't change though). - AD7six
Additional: if you've got 1000s of pages that you're trying to cache statically - you're probably much better off generating them as part of your deployment processes (i.e. requests for these pages never hits php on the deployed site). - AD7six

1 Answers

1
votes

I was able to vastly reduce the size of cached view files in the end by making these two changes. Hopefully it is useful to anyone who runs into a similar issue.

1 - Model

There was a number of relationships between models, however I was only actually making using of the data on one side of the relation. For example, an article had images, but I also had a relation to get the article belonging to an image, which I never did. Making the relations 'one way' i.e. as required cut down on heavy queries.

2 - Controller

In my paginate array I had 'recursive'=>2, which was causing hundreds of extra queries per page. I was able to cut the queries on a heavy page down from 900 to 20 by changing this option to 'recursive'=>1. There was about 6 many to many relations on the model in question. This recursion level must have slipped in at some stage inadvertently.

I still feel it is odd for CakePHP to serialize PHP in the cached view files. A more optimal approach would be static HTML files without any PHP.