CakePHP cache workarounds (part 1)

Posted by Matt on Wed, Feb 21 2007

I've been messing around with CakePHP's view caching recently and hit a couple bugs. They've both been posted in the tracker, but remain unfixed at the moment.

The first issue occurs when you try to cache the main page of your site - whatever appears when you go to www.site.com. This was broke in changeset 4269. Before this change the default page would get saved in the cache folder as ".php". Not an ideal file name, but at least it worked. With the addition of this change the method returns without writing the cache file.

Someone suggested a fix for this in the CakePHP Google groups, but the solution only partially works. Firstly putting the two ReWrite rules in .htaccess above the generic CakePHP rule screws up the loading of css files - I don't understand exactly why. Moving the two rules to the bottom does allow for the page to be loaded w/ css and the cached view is written to the disk.

HOWEVER, the cached view is never used and will just be re-written on every page hit. This is because the code that determines the cached file name is different when reading the file then when writing the file. The reading code uses a function called setUri. The return for this function is dependent on the server setup, but in my case it returns "/", which doesn't match the cache file written after using the ReWrite suggested above.

THE SOLUTION: You have to actually redirect the browser to the correct page so that address changes. To do that put this rule in your .htaccess AT THE BOTTOM.


    RewriteRule ^$	/controller/action [R,L]
    RewriteRule ^/$	/controller/action [R,L]

This does generate an extra request due to the redirect. The optimal solution is to fix this in the CakePHP core. Once I have a better understanding of the cache code I may suggest a fix, assuming someone doesn't beat me to it.

Posted in CakePHP

1 Comment

Matt said on Jan 02, 2008
This is fixed as of the 1.2.6311 Beta release. YES!

Add new comment