Optimize CakePHP Web Application By Element Caching

Hello Friends, I have written some articles relates to element and its use for an application that how cakePHP elements can be useful for web application. Now today I am again want to writing about cakePHP element for web application optimization. As we know that Many applications have small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout. CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements. So when we use reusable elements in any application and that element having any interaction with database to retrieve any thing then it is obvious that every time when that element load with page it interacts with DB to retrieve data, But as elements are reusable parts so we can cache that reusable parts . That means if we use element caching then it will not interact with database eveytime, It will interact first time and after that automatically take from cache, So this thing will not interact with your database eveytime and in this way you can optimized cakePHP application via element caching.

You can take advantage of CakePHP view/element caching if you supply a cache parameter. If set to true, it will cache for 1 day. Otherwise, you can set alternative expiration times. See Caching for more information on setting expiration.
<?php echo $this->element('helpbox', array('cache' => true)); ?>

Your default caching must be enabled for element caching to be worked.

If you render the same element more than once in a view and have caching enabled be sure to set the ‘key’ parameter to a different name each time. This will prevent each succesive call from overwriting the previous element() call’s cached result. E.g.
<?php
echo $this->element('helpbox', array('cache' => array('key' => 'first_use', 'time' => '+1 day'), 'var' => $var));

echo $this->element('helpbox', array('cache' => array('key' => 'second_use', 'time' => '+1 day'), 'var' => $differentVar));
?>

The above will ensure that both element results are cached separately.

Optimize CakePHP Web Application By Element Caching
  • Asd

    Totally useless, because the same examples exists in the Cakebook…

Subscribe to PHP Freelancer

Enter your email address: