Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePhp speed optimization

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

like image 839
user1871640 Avatar asked Dec 02 '25 05:12

user1871640


2 Answers

Here is something I would check if the site is performing slow

Speed Optimization

  1. Enable caching
  2. Compress JS and CSS. (A nice plugin that does this)
  3. A good speed optimization checklist

Cake Practices

  1. Cake conventions are your best guidelines, the framework has been designed to be scaled with its conventions.

  2. 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.

  3. 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.

  4. You can also drop needs of counter query for related data by using Counter cache. More Details here

  5. Cache your view

  6. 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.

like image 62
Kishor Kundan Avatar answered Dec 06 '25 00:12

Kishor Kundan


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.

like image 43
mark Avatar answered Dec 05 '25 23:12

mark