in Cakephp tips minutes

The Importance of a Clean Cache

This is quick tip to remember when you have a production site you’ve had running for a while or a development site you’ve been running on production mode. It’s an easy thing, but it can bite you if you don’t know how Cake works.

TL & DNR : Clear out those folders under/tmp/cache  anytime you modify a database table.

CakePHP stores a lot of there, in the /cache/models  directory is all the meta data it generates on a table that it needs but would hurt performance if it was generated each time. Now in debug mode it doesn’t do that step, because, well you’re working away and the database structure is going to probably change a bit. Now once you turn debug off, CakePHP is going to generate those files so it’s not doing a lot of stuff like this:

sql-log-no-cache

As you can see there’s a lot of SQL going on that has nothing to do with what your app is trying to do; in this case it’s the built-in pagination that’s been baked for a table containing articles. Obviously it eventually gets around the querying the table, but there’s a lot of set-up before all the Cake magic happens.

sql-log-cache

Now here’s the SQL being run after the cached table has been generated – looks a lot more like you’d expect to see.

But the thing to remember is that whenever you update your table structures that code in /tmp/cache needs to be re-generated otherwise Cake will keep trying to interact with the old table structure. Mind you, this was more of a problem with Cake 2, Cake 3 seems to be a bit smarter about handling table structure changes, but it’s something to keep in mind if you upload a new version of your site and it seems like table fields are missing – clear out those /tmp/cache/models/myapp_cake_model_default_… files and you might save yourself some grief!