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'),
$this->Contest->getEnumValues("contest_category"));
cache($cache_name, serialize($list), $cache_expires);
} else {
$list = unserialize($cache_data);
}
return $list;
}



getEnumValues is something I got as a snippet from cakeforge.
http://cakeforge.org/snippet/detail.php?type=snippet&id=112



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();
debug($categories);
}



-Mandy.

Monday, February 12, 2007

Update: Inype Editor - New Release

So, the guys at Intype have come out with their next major release - 0.2.1.
This release is pretty cool and has lots of exciting features the 2 most important ones being drag and drop support and the ability to do Undo/Redo.

Here is the feature set for this release -

  • Undo/Redo
  • Reload Bundles — A huge help for bundle creators. Bundles can now be reloaded directly from the Intype UI, there’s no need to restart the whole application.
  • CJK Support — Support for Chinese, Japanese and Korean over-the-spot input methods. I’d like to send a big thanks to Mr. Masahide Morio for teaching me the basics of Japanese.
  • Tab/Space Switch Level 1 — Includes switching between hard and soft tabs (tab chars versus spaces). Conversions and automatic detection are planned for Level 2, that will be released as and update to 0.2.1.
  • Drag/Drop File Support Level 1 — In response to many user requests. Level 1 supports dragging/dropping files to open. More to come with scripting support
  • Editing Features — Move line(s) up and down; Duplicate line(s); Transpose characters; Vertical scrolling with keyboard; New variables for snippets.
It's getting better. Like using it with the cakephp bundle.

-Mandy.