Configuring Result Cache in Symfony 1.4 using APC
February 16th, 2010
No comments
Pulled from “More With Symfony” : http://www.symfony-project.org/more-with-symfony/1_4/en/08-Advanced-Doctrine-Usage.
Configuring Result Cache
In order to use the result cache we need to configure a cache driver for the queries to use. This can be done by setting the ATTR_RESULT_CACHE attribute. We will use the APC cache driver as it is the best choice for production. If you do not have APC available, you can use the Doctrine_Cache_Db or Doctrine_Cache_Array driver for testing purposes.
We can set this attribute in our ProjectConfiguration class. Define a configureDoctrine() method:
class ProjectConfiguration extends sfProjectConfiguration
{
// ...
public function configureDoctrine(Doctrine_Manager $manager)
{
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc());
}
}
Here’s how it’s used (note useResultCache()):
$q = Doctrine_Core::getTable('User')
->createQuery('u')
->orderBy('u.username ASC');
$q->useResultCache(true, 3600, 'users_index');
To check if the cache exists you’d do this:
$manager = Doctrine_Manager::getInstance();
$cacheDriver = $manager->getAttribute(Doctrine_Core::ATTR_RESULT_CACHE);
if ($cacheDriver->contains('users_index'))
{
echo 'cache exists';
}
else
{
echo 'cache does not exist';
}