Quick and Dirty Caching for eZJSCore (eZ Publish)
Friday, February 26, 2010 8:07:49 AM
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] );
$node->ContentObject->fetchDataMap();
// 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.















