Now:
$query = $this->getEntityManager()
->createQuery(
'SELECT s FROM AcmeDemoBundle:Service s
WHERE s.status = :serviceStatus
');
$query->setParameter('serviceStatus', Service::STATUS_ENABLED);
$query->useResultCache(true, 10, 'findActiveServices');
The idea is centralize the cache mechanism and the query creation, into the configuration file store the cache mechanism and the dql.
RFC configuration file example yaml
#'Acme\Bundle\CityTour\ProductBundle\Repository\CityTourRepository\findActiveCTServices':
findActiveCTServices:
resultLifetime: 10
resultCache: true
queryCache: false
dql:'SELECT s
FROM AcmeDemoBundle:Service s
WHERE s.status = :serviceStatus'
And the repository will be:
$query = $this->getEntityManager()
->createQueryByIdentifier('findActiveCTServices');
$query->setParameter('serviceStatus', ProductStatus::ENABLED);
$query->setParameter('serviceTypeCode', 'CT');
// implicit!!! $query->useResultCache(true, 10, 'findActiveCTServices');
the ResultCacheId if the parameters are not empty will be something like
'findActiveCTServices' .'__' . serialize($query->getParameters())