Skip to content

Instantly share code, notes, and snippets.

@peterjmit
Created March 6, 2012 17:47
Show Gist options
  • Save peterjmit/1987713 to your computer and use it in GitHub Desktop.
Save peterjmit/1987713 to your computer and use it in GitHub Desktop.
Get the Result Cache from the doctrine entity manager
<?php
class Foo
{
private $_em;
public function __construct($entity_manager)
{
$this->_em = $entityManager;
}
/**
* I couldn't find any where in the Doctrine or Symfony docs that explained
* how to get the Cache Driver defined in my config_prod.yml file for use
* in saving custom arrays/keys.
*
* (Rather than directly instantiating a cache driver i.e. $cacheDriver = new \Doctrine\Common\Cache\ApcCache())
*
* @return Implementation of Doctrine\Common\Cache\Cache (APC/Xcache etc.)
*/
public function getCacheDriver()
{
return $this->_em->getConfiguration()->getResultCacheImpl();
}
/**
* Example usage of Foo::getCacheDriver
*
*/
public function saveToCache($item_to_save)
{
$this->getCacheDriver()->save('foo_key', $item_to_save);
}
}
$foo = new Foo($doctrine_entity_manager);
$foo->saveToCache('bar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment