Bu örnek doctrine/cache paketi üzerine kuruludur ve ne uygulama içinde ne de işletim sisteminde herhangi bir konfigürasyon gerektirir. Örneğimiz APC'yi kullanıp nasıl performansı arttıracağımızı gösterecek. Daha fazla bilgi için doctrine caching chapter ve Alternative PHP Cache sayfalarını ziyaret edebilirsiniz.


controllers.yml


services:
application_backend.controller.football:
class: Application\BackendBundle\Controller\FootballController
arguments:
- @templating
- @application_backend.repository.league
- @doctrine.orm.entity_manager
- @application_backend.service.apc_cache

services.yml


services:
application_backend.service.apc_cache:
class: Doctrine\Common\Cache\ApcCache

repositories.yml


services:
application_backend.repository.league:
class: Application\BackendBundle\Repository\LeagueRepository
factory: [@doctrine.orm.entity_manager, getRepository]
arguments: [Application\BackendBundle\Entity\League]

Controller


namespace Application\BackendBundle\Controller;

use Application\BackendBundle\Repository\LeagueRepository;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

/**
* @Route("football", service="application_backend.controller.football")
*/
class FootballController extends Controller
{
private $templating;
private $leagueRepository;
private $entityManager;
private $apcCache;

public function __construct(
EngineInterface $templating,
LeagueRepository $leagueRepository,
EntityManagerInterface $entityManager,
ApcCache $apcCache
) {
$this->templating = $templating;
$this->leagueRepository = $leagueRepository;
$this->entityManager = $entityManager;
$this->apcCache = $apcCache;
}

/**
* @Method({"GET"})
* @Route("")
*/
public function indexAction()
{
$cacheId = 'find_all';
if ($this->apcCache->contains($cacheId)) {
$result = $this->apcCache->fetch($cacheId);
} else {
$result = $this->leagueRepository->findAll();
$this->apcCache->save($cacheId, $result, 60);
}

return $this->getTemplate(['result' => $result]);
}

/**
* @param array $parameters
*
* @return Response
*/
private function getTemplate(array $parameters = [])
{
return $this->templating->renderResponse(
'ApplicationBackendBundle:Football:index.html.twig',
$parameters
);
}
}

Repository


namespace Application\BackendBundle\Repository;

use Doctrine\ORM\EntityRepository;

class LeagueRepository extends EntityRepository
{
public function findAll()
{
$qb = $this->createQueryBuilder('l')
->select('l, t, p')
->innerJoin('l.team', 't')
->innerJoin('t.player', 'p')
->orderBy('l.name', 'ASC')
->addOrderBy('t.name', 'ASC')
->addOrderBy('p.name', 'ASC')
->getQuery();

$qb = $qb->getResult();

return $qb;
}
}

Twig


{% extends '::base.html.twig' %}

{% block body %}
{% spaceless %}
{{ dump(result) }}
{% endspaceless %}
{% endblock %}

Test


Uygulamaya http://football.local/app_dev.php/backend/football adresinden ulaşıyoruz. Cache aktivitilerini görmek isterseniz APC-Admin kullanabilirsiniz.


Analiz


  Millisecond MB
3514 40.5 (*) query run
425 22.5
434 22.5
712 28.5 (*) query run
627 22.5
434 22.5
427 22.5

Cache bilgileri