About cache and eager loading in laravel, I wrote in my previews post.
Now we add caching functional on page 'posts' in phalcon framework.
Let`s add new Controller:
PostwsController() in phalcon-3.2/app/controllers/PostswcController.php
use Phalcon\Cache\Backend\File as BackFile;
use Phalcon\Cache\Frontend\Data as FrontData;
/**
* Index action
*/
public function indexAction()
{
$posts = null;
$frontCache = new FrontData(
[
'lifetime' => 172800,
]
);
$cache = new BackFile(
$frontCache,
[
'cacheDir' => '../app/cache/',
]
);
$cacheKey = 'posts.cache';
$posts = $cache->get($cacheKey);
if ($posts === null) {
$this->persistent->parameters = null;
$parameters = $this->persistent->parameters;
if (!is_array($parameters)) {
$parameters = [];
}
//$parameters["limit"] = "100";
//$parameters["limit"] = array('number' => 100, 'offset' => 10);
$parameters["order"] = "id";
$posts = Posts::find($parameters);
$cache->save($cacheKey, $posts);
}
if (count($posts) == 0) {
$this->flash->notice("The search did not find any posts");
$this->dispatcher->forward([
"controller" => "postsCache",
"action" => "index"
]);
return;
}
//var_dump($posts);
$this->view->posts = $posts;
}
Views we can copy from posts to postswc:
phalcon-3.2/app/views/layouts/posts.phtml -> phalcon-3.2/app/views/layouts/postswc.phtml
folder phalcon-3.2/app/views/posts -> folder phalcon-3.2/app/views/postswc
And that`s all. New page 'postswc' we can see by link
After tests we see that adding a cache did not change the situation. In table !!phalcon-postswc row.
| framework | requests per second | relative | peak memory | relative |
|---|---|---|---|---|
| phalcon-index | 113.17 | 2,829.3 | 0.49 | 1.0 |
| phalcon-authors | 22.35 | 558.8 | 0.61 | 1.3 |
| phalcon-categories | 13.61 | 340.3 | 0.62 | 1.3 |
| laravel-index | 8.67 | 216.8 | 6.21 | 12.7 |
| laravel-posts-el-cache | 8.00 | 200.0 | 7.34 | 15.1 |
| laravel-posts-el | 7.79 | 194.8 | 7.34 | 15.1 |
| laravel-categories | 6.97 | 174.3 | 7.59 | 15.6 |
| laravel-authors | 6.90 | 172.5 | 7.34 | 15.1 |
| !! phalcon-postswc | 0.10 | 2.5 | 2.45 | 5.0 |
| phalcon-posts | 0.09 | 2.3 | 2.36 | 4.8 |
| laravel-posts | 0.04 | 1.0 | 15.13 | 31.0 |
| laravel-posts-cache | 0.04 | 1.0 | 18.46 | 37.9 |
eager loading next time, because there is not eager loading in Phalcon from the box.For every next release it's testing results will be commited in repository (bm/result-releases/0.0.5/output, bm/result-releases/0.0.6/output, bm/result-releases/...../output, etc.)
And they can be viewed on http://php-frameworks.semasping.info/bm/result-releases/0.0.6/index.php where 0.0.6 - release version.
That links will be added to release informations on github
stibiumz/phalcon.eager-loading for Phalcon 1.3.* - 2.0.*.)