Reputation: 18660
Having this entity:
/**
* @ORM\Entity
* @ORM\Table(name="nomencladores.pais", schema="nomencladores")
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\PaisRepository")
* @UniqueEntity(fields={"nombre"}, message="El pais ya existe en nuestra base de datos")
*/
class Pais
{
use IdentifierAutogeneratedEntityTrait;
use NamedEntityTrait;
use ActiveEntityTrait;
}
And this Repository:
class PaisRepository extends EntityRepository
{
public function getPaises()
{
$qb = $this->createQueryBuilder('pais');
$query = $qb->getQuery();
$query->useResultCache(true, 3600, 'paises_cache');
return $query->getResult();
}
}
And doing this call on my controller:
/**
* @Route("/paises", name="paises")
* @Method("GET")
*/
public function getPaisAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:Pais')->getPaises();
$response['message'] = "";
$response['entities'] = [];
if (!$entities) {
$response['message'] = "No se encontraron países";
}
foreach ($entities as $pais) {
$paises = array();
$paises['id'] = $pais->getId();
$paises['text'] = $pais->getNombre();
$response['entities'][] = $paises;
}
return new JsonResponse($response);
}
And having this at config_prod.yml
file:
doctrine:
orm:
metadata_cache_driver: apc
result_cache_driver: apc
query_cache_driver: apc
How do I know if my app is using cache results or is doing a DB query all the time? I'm seeing into Symfony2 toolbar (because I'm performing test at dev environment) and all the time I'm seeing the query for paises
table which makes me think no cache is used.
Upvotes: 0
Views: 46
Reputation: 19750
I believe that you have to tell Doctrine when to use the configured caching driver, like so:
public function getSomeEntities()
{
$qb = $this->createQueryBuilder('a');
$qb
->where('a.title = "test article"')
->orderBy('id', 'ASC')
;
$query = $qb->getQuery();
$query->useResultCache(true, 60, 'uniqueCacheId');
return $query->getResult();
}
You can also specify the use of query caching:
$query->useQueryCache(true);
You can see in the Doctrine\ORM\Query class documentation that the useQueryCache
function accepts a single boolean parameter and the useResultCache
has the following parameters:
useResultCache( boolean $bool, integer $timeToLive = null, string $resultCacheId = null )
Upvotes: 1