Enhanced Caching of Views in CakePHP 1.2

Image borrowed from James Fee’s blog

It’s good to see that CakePHP developers worked on making Caching mechanism more effective and usable. In CakePHP, you can either 1) cache your data in controller or 2) cache your views (whole view or elements). Jiri talks in detail on how to cache data on controller side. So I won’t discuss that here.  Here I am just reporting how the latest version of CakePHP ( pre-beta) solves a bug (or problem) of caching an element for different users.

Unless you hacked the CakePHP core library, earlier versions of CakePHP had no way to create cache of an element for different users. For instance, consider an element, “user_interest.ctp”, that displays interest areas of  a user. Since each user has a different interest areas, there should be a different cache file for each user. So for user one (id = 1), the cache file should be named as element_1_user_interest. Similarily, for user two (id = 2), the cache file should be named as element_2_user_interest.

However, in earlier versions of CakePHP, there was no way to provide user id to caching mechanism. But in CakePHP 1.2 ( pre-beta release), CakePHP developers have implemented a mechanism where you can provide a unique identifier to caching mechanism, making it now possible to distinguish cache of an element for different users. Below is the sample code that shows how to set a unique identifier

//This code goes in your view
$this->element(‘user_interest’, array(
                                                                      ‘cache’=>array(‘time’=>”+7 day”, ‘key’=>”{$user_id}”, /* lets say $user_id = 1 */
                                                                      ‘user_data’ => $data[‘interest’]

As desired, this will create a cache file with the following name: element_1_user_interest, where 1 is the unique identifier corresponding to user id.

Great Work CakePHP developers (and thanks for listening to user community)


About Ritesh Agrawal

I am a applied researcher who enjoys anything related to statistics, large data analysis, data mining, machine learning and data visualization.
This entry was posted in CakePHP and tagged . Bookmark the permalink.

4 Responses to Enhanced Caching of Views in CakePHP 1.2

  1. daniel says:

    nice article. thx!

  2. Tristan says:

    Any idea how to cache views that are protected by Auth and have them remain protected?

  3. ragrawal says:

    One simple idea would to append user id as the filename and before you retrieve the cache make sure that the same user is logged in.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s