Tuesday, February 20, 2007

Caching with cakePHP

cakePHP has view caching built in which when used cleverly will really speed up your applications.

However, this doesn't work with requestAction.

Let's consider this example:

1. You store all your categories in a database.

2. While displaying your views, you want to pull these categories from the DB.

3. You don't want that every time you display a page, you should pull the categories from DB. Since they are not going to change (till you change them :) it's an unnecessary hit on the DB.

4. You want to load these categories at startup and then just keep pulling the data from the cache rather than the DB.

There can be many ways of doing this, but one of them could be that whenever the category data is first requested, we hit the DB and get the list of categories, then cache them (in a file) and subsequent requests would be served from reading the file (it could be stored in the session too).

So, I decided to give this a shot. cakePHP already has a cache() function that can be used for achieving something like this.

Here is what I came up with:

function __getCategories()
$cache_name = "views".DS."categories-list.php";
$cache_expires = '+24 hours';

$cache_data = cache($cache_name, null, $cache_expires);
if (empty($cache_data))
$list = array_merge(array('categories'=>'All Categories'),
cache($cache_name, serialize($list), $cache_expires);
} else {
$list = unserialize($cache_data);
return $list;

getEnumValues is something I got as a snippet from cakeforge.

function getEnumValues($columnName=null)
if ($columnName==null) { return array(); } //no field specified

//Get the name of the table
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$tableName = $db->fullTableName($this, false);

//Get the values for the specified column
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");

//figure out where in the result our Types are
//(this varies between mysql versions)
$types = null;
if ( isset( $result[0]['COLUMNS']['Type'] ) )
$types = $result[0]['COLUMNS']['Type'];
} //MySQL 5
elseif ( isset( $result[0][0]['Type'] ) )
$types = $result[0][0]['Type'];
} //MySQL 4
else {
return array();
} //types return not accounted for

//Get the values
$values = explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types) );

//explode doesn't do assoc arrays, but cake needs an assoc to assign values
$assoc_values = array();
foreach ( $values as $value ) {
//leave the call to humanize if you want it to look pretty
$assoc_values[$value] = Inflector::humanize($value);

return $assoc_values;

} //end getEnumValues

So, that's it, you can now do the following in your controller:

function test()
$categories = $this->__getCategories();