When you developing a website, more often than not you have small blocks of presentation code that need to be repeated from page to page. In CakePHP they have Elements to help you with it. It’s all very fine but when you have to pull data from database and show it on the element the real problem begins. Now you don’t import the model (I prefer importing model than controller) on every controller’s actions and set it for the view because it will take a lot of time to do that and mostly i think this is a very stupid. Another way to solve this problem is to use requestAction and i hate requestAction.
So what to do? Fear not CakePHP have a solution – a very sweet solution. Most importantly a very simple solution. CakePHP has a very good Cache Utility Library. You can use File, Apc, Xcache, Memcache as engine. I am going to show you how to use File caching. Now you have to add some configuration for caching time in core.php
// File engine caching // short Cache::config('short', array( 'engine' => 'File', 'duration'=> '+2 hours', 'probability'=> 100, 'path' => CACHE, 'prefix' => 'cake_', 'lock' => false, 'serialize' => true) ); // medium Cache::config('medium', array( 'engine' => 'File', 'duration'=> '+1 day', 'probability'=> 100, 'path' => CACHE, 'prefix' => 'cake_', 'lock' => false, 'serialize' => true) ); // long Cache::config('long', array( 'engine' => 'File', 'duration'=> '+1 week', 'probability'=> 100, 'path' => CACHE, 'prefix' => 'cake_', 'lock' => false, 'serialize' => true) ); // Very long Cache::config('very_long', array( 'engine' => 'File', 'duration'=> '+30 days', 'probability'=> 100, 'path' => CACHE, 'prefix' => 'cake_', 'lock' => false, 'serialize' => true) );
Now we are going to use medium as our default
Cache::config('default', Cache::settings('medium'));
Our setup is done. Now we are going to use it in our controllers. I generally check for caching file in beforeFilter() in app_controller.php.
// this is a cool function which comes useful in many cases function beforeFilter() { if (($announcements = Cache::read('announcements', 'long')) === false) { App::import('Model', 'Announcement'); $announcement = new Announcement(); $announcements = $announcement->find('all', array( 'order' => 'Announcement.modified desc' )); Cache::write('announcements', $announcements, 'long'); } }
What happens here is I first look for announcements caching file if I don’t found one, I create. Now suppose you edited an announcement so your cache have to be edited. what I do it delete the previous caching file and create new one.
Cache::delete('announcements'); $announcements = $this->Announcement->find('all', array( 'order' => 'Announcement.modified desc' )); Cache::write('announcements', $announcements, 'long');
Now to showing the data is very easy.
$announcements = Cache::read('announcements','long'); foreach($announcements as $announcement): echo $announcement['Announcement']['title']; endif;
I use Memcache when i can, i love the way cake is so easy to switch caching on and off and between different vendors its great. Nice examples too!
Me two. But most of the time i have rely on File caching which pretty good.
Very nice, helped me a lot 🙂