Lordi Uusipaavalniemi

Learn the sweetness of my mercy

Quick and Dirty Caching for eZJSCore (eZ Publish)

eZJSCore is an extension for eZ Publish that aims to help developers in implementing backends for AJAXy interfaces. As of now (eZ Publish 4.2 and 4.3) the eZJSCore extension of does not support caching at all. Read more about using the extension at share.ez.no.

For some dynamic interfaces it makes sense to have caching for the AJAX calls. Since eZ Publish requires the eZ Components component library it is natural that we would use it for caching.

I built a quick and dirty caching method using the eZ Components Cache Component. At the moment it requires APC, but if you want to have a backup, just see the documentation at the documentation page. At the moment there is no automatic purging of cache on content publish, just a TTL of 60 seconds. This should be feasible to implement using a content edit handler.

Here's the code in all it's simplicity:

public static function get_node( $args ) {

	// set cache key
	$cache_key = 'ezjscore-get_node-';
	// add keys
	foreach($args as $arg){
		$cache_key .= $arg;
	// set cache options
	$options = array(
	  'ttl'   => 60
	// init cache
	ezcCacheManager::createCache( 'apc', 'apc', 'ezcCacheStorageApcPlain', $options );
	$cache = ezcCacheManager::getCache( 'apc' );
	// check if node is available in cache
	if ( ( $node = $cache->restore( $cache_key ) ) === false ){
		// fetch node and datamap
		$node = eZContentObjectTreeNode::fetch( $args[0] );
		// store node to cache
		$cache->store( $cache_key, $node );  									
	echo ezjscAjaxContent::nodeEncode($node);

If you're a finnish speaker and want to know more about eZ Publish, check out my quick introduction to eZ Publish in Finnish. Scheren für Linkshänder.

Written by Unknown on Friday February 26, 2010

Permalink -

« Benchmarking WordPress on HipHop for PHP - Emo Hitler - He killed over six million jews just to see if he could feel ANYTHING. »