My app is working very slow. I am using Cakephp 2.3.1 version. Will it be beneficial to load Model, Components and Helpers in the functions where they are needed? Right now I am calling them in class. eg:
class PanelsController extends AppController {
public $name = 'Panel';
public $uses = array(list of models goes here);
public $components = array(list of components goes here);
.................
}
What other techniques do you suggest. Thanks
Here is something I would check if the site is performing slow
Speed Optimization
Cake Practices
Cake conventions are your best guidelines, the framework has been designed to be scaled with its conventions.
Recursion and Containable, By default Cake fetches all related data when a query is fired. Both recursive level and containable behavior can limit the amount of data retrieved. If cake fetches all related data by default doesn't mean that you have to keep it that way.
Keep your DB normalized. This will allow you to defer many processes. For eg. when retrieving posts, cake automatically fetches all of its related data (Tags, comments). But when you have higher-order normalized DB, u can defer loading comments from an XHR/AJAX request. This will also allow you to serve comment related logic from comment's Model, Controller and View. Even if you bring related model data set limits for them.
You can also drop needs of counter query for related data by using Counter cache. More Details here
Cache your view
You can cache query results manually too,
Cache::write($this->Post->find("all"));
Try them out and you should be able experience amazing speed improvements.
Lastly, I do believe that Architecture of an application plays a big role in performance. Some times, we have to separate certain logic from a request Life cycle to boost the performance.
public $uses() does not matter. you can add as many as you want. Cake will only lazyload them if needed somewhere.
Just make sure you got recursive = -1 per default in your AppModel and only raise it or contain data you really need.
But your components will all be loaded and initialized right away. You might want to reduce those.
Those two attributes cannot be your bottleneck, though. You must have some other serious issues.
Also don't make assumptions in debug mode. The real speed is measured/obseved with debug 0 where no additional debug info is gathered and the cache is not constantly replaced.
EDIT: Please note that my words above are only meant from a "speed point of view". It does not matter for speed. But it is not recommended to add models in $uses if you are able to reach those via relations and the relation chain.
So let's say you want to make a dashbard. In most cases you only need to add "User" model, since Profile, Images, and other models are usually directly accessable via $this->User->Profile->foo($bar) etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With