I recently built a mobile friendly website using a responsive CSS framework. This worked great and it all fluidly slid into place on various devices. However, in order to reduce the load time on the mobile version I also used a mobile detect script to reduce the amount of images served. This also worked great.
The problem: Just to speed things up a little more I decided to use Codigniter’s native caching. Job done!
Well no actually. Today I looked at the homepage and noticed half of it was missing. 20 seconds later the penny dropped – this was a cached view of the mobile version. After a little research I discovered Codeigniter caches by view (you can also cache database queries, but with no inbuilt garbage collection I don’t use it).
The solution: I really didn’t want to have to turn of caching so a little more research. This came up with Phil Sturgeon’s caching library. So far it works like a charm. Instead of caching the user’s view it is far more granular allowing the data from models to be cached. So instead of calling the model and having it query the database every time you cache this. The code looks like this:
//Uncached model call $this->test_model->get_posts($category_id, 'live');
//Cached model call $this->cache->model('test_model', 'get_posts', array($category_id, 'live'), 120); // keep for 2 minutes
Whilst using this method doesn’t quite reduce the dynamic load as much as caching the whole view, in the given scenario it is a pretty good compromise.
Another case this method would be useful is in caching logged in and not logged in versions of a page.