smartcoderx
smartcoderx

Reputation: 1081

Symfony2 and Doctrine APC Cache

i have read the documentation of symfony2 in relation to the performance and I have realized the following steps.

  1. Install APC 'php-apc' on my webserver and restart my webserver
  2. Modify my doctrine configuration

    doctrine:
    
    dbal:
    
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
    
       orm:
    
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        metadata_cache_driver: apc
        result_cache_driver: apc
        query_cache_driver: apc
    

Now if i call a action to retrieve all users from database i see in the information bar at the bottom that doctrine execute every time 114 queries. Why the queries not cached?

My action look like this:

$users = $this->getDoctrine()->getRepository('AppUserBundle:User')->findAll();
return $this->render('AppUserBundle:User:index.html.twig', array('users' => $users));

Upvotes: 1

Views: 1369

Answers (1)

Cyprian
Cyprian

Reputation: 11374

Doctrine doesn't cache query results by default. You have to explicitly point that you want to cache query using useResultCache method. For example, if you'd like to cache getting all users, write your own method in User repository class:

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{

    public function fetchAll()
    {
        $query = $this->createQueryBuilder('u')->getQuery();

        return $query->useResultCache(true)->getResult();
    }
}

The method may take additional arguments:

public function useResultCache($bool, $lifetime = null, $resultCacheId = null)
  • $bool - set to true if you want to cache query result
  • $lifetime - TTL of cached result in seconds
  • $resultCacheId - you can pass your own id, in case of null Doctrine will handle that

Upvotes: 4

Related Questions