umm... silly question.
I know that one can (and should) use APC cache driver with Doctrine ORM. But I was just wondering if I can also cache entity manager instance itself? Is there anything that prevents me from doing this:
$em = apc_fetch('em');
if(!$em){
$cache = new Doctrine\Common\Cache\ApcCache;
$config = new Doctrine\ORM\Configuration;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$config->setAutoGenerateProxyClasses(TRUE);
$conf开发者_开发技巧ig->setProxyNamespace('MyProject\Proxies');
$config->setProxyDir(APP_PATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'proxy');
$driverImpl = $config->newDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
$config->setMetadataDriverImpl($driverImpl);
$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => 'database.sqlite'
);
$em = Doctrine\ORM\EntityManager::create($connectionOptions, $config);
apc_store('em', $em);
}
Your code should work just fine. Actually we also do cache EM in our projects. Saves some 300Kb:)
I had the same problem and resolved this way:
$em=unserialize($_SESSION['entityManager']);
$_SESSION['entityManager']=serialize($entityManager);
(Actually, I serialize a self developed DAOFactory object, which in turn contains the entitymanager).
That seems to work fine for me.
BTW: I'm not using APC cache because I want every single user to have a separated instance of all the DAO's entityManager and DBALConnection objects.
精彩评论