Caching really speeds up your webapp ?
Before go to live your application , things to do:
- Change debug mode 2 to 0
-
Configure::write('debug', 2) to Configure::write('debug', 0).
Not only for speed improvement. It is an absolute requirement for every cakephp web Application.
So final code for caching is :
Here we have used default caching FileCache engine...You can change Cache enging in config/core.php
- Recursive Find statements
It's good feature of Cakephp don't waste you speed only in recursive mode...
Exp: When user have multiple blogs ,if simple use find statement to find user list you will also get all blogs list with users..Means you are getting unuseful data with joins...This really decrease speed of App.
so override default recursive with -1 in AppModel and change whenever you need to change it
Use $this->ModelName->find('list'); when need only simple list id & name class AppModel extends Model {
public $recursive = -1;
}
- Get rid of var $uses
Cutting down on the
Use loadModel dynamically whenever it requires
$uses
array helps a little ..
Use loadModel dynamically whenever it requires
$this->loadModel('ModelName');
- Unbind Models
A good practice is to always specify the fields you need in Find. Also, for spectific queries, you should unbind associated models you don't need.
For Complex queries we can go with query() ...
- Normalize your database tables
And at last use caching for complex & regulor queries
There are different type of caching cakephp provides..
- Filecache
- ApcCache
- Wincache
- XchacheEngine
- MemcachEngine
- MemcachedEngine
- RedisEngine
But in this post i am going with Simple but little bit slow FileCache engine
This is time for an real example of where & how to use Cakephp caching
Exp.
We are going to make a Travel website...
On Home page we have listed most recently added tout packages....
so we know recent tours are dynamic but doesn't changes frequently..so we add latest tours in caching & whenever home page calls we can show them directly from caching no need to make database connection every time...
Code to write cache :
Cache::write('recent_tours', $recent_tours); //$recent tours contains list of recent tours
Whenever we need recent_tours list we can access from caching..
$recent_tours = Cache::read('recent_tours'); //get $recent tours list from caching
But the problem is how cache will know when admin will add a new tours package ?
Yes Cakephp provide simple Model Callback methods for us to know this..
Yes Cakephp provide simple Model Callback methods for us to know this..
function afterSave(boolean $created, array $options = array()) {
//we can update our caching here after add & update records
}
function afterDelete() {
// we can update our caching file after delete records
}
So final code for caching is :
//Add this code in controller where you need to display recent packages
$recent_tours= Cache::read('recent_tours'); // get recent tours if cache avalialbel
if(!$recent_tours) { // if cache deleted in model after update records it will create new caching
$recent_tours= $this->Tour->find('list',array(
fields' => array('name','description')
));
Cache::write('recent_tours',$recent_tours);
}
//Add this code in model for that you writing caching
function afterSave($created, $options = Array() ) {
Cache::delete('recent_tours');
}
function afterDelete() {
Cache::delete('recent_tours');
}
Here we have used default caching FileCache engine...You can change Cache enging in config/core.php
Cache::config('default', array(
'engine' => 'File', //[required]
'duration' => 3600, //[optional]
'probability' => 100, //[optional]
'path' => CACHE, //[optional] use system tmp directory - remember to use absolute path
'prefix' => 'cake_', //[optional] prefix every cache file with this string
'lock' => false, //[optional] use file locking
'serialize' => true, //[optional]
'mask' => 0664, //[optional]
));
Please don’t forgot to comment you views & errors…It will be helpful to improve this blog..