title: Symfony2 - How to set Doctrine APC cache prefix with multiple apps
body: <p><strong><em>Update: See bottom of post</em></strong>
  ( this was orginially posted when we were using SF 2.3, we're now on 2.7.0 )</p>
  
  <p>We've got multiple Symfony apps running on our server, but we appear to be getting cache pollution across the sites due to all the doctrine ORM cache entries being prefixed with 'sf2'.</p>
  
  <p>From spending the last 30 mins googing around, there doesn't appear to be an easy solution.</p>
  
  <p>The first ones suggested using the ProjectConfiguration.class.php - but that appears to be a Symfony 1 thing.</p>
  
  <p>The next possible solution was to use the ApcUniversalClassLoader class (as per this question on SO: <a href="https://stackoverflow.com/questions/11395298/multiple-symfony2-sites-using-apc-cache">Multiple Symfony2 Sites using APC Cache</a>).</p>
  
  <p>The problem is that our setup uses composer to autoload, so we can't simply use the code as per that example.</p>
  
  <p>The class caching side of APC is fine, you can set the key in the front controller, but it doesn't set the key for the doctrine cache.</p>
  
  <p>Has anyone got any thoughts please as currently we're having to disable APC for doctrine on all but the first app.</p>
  
  <p>Frontcontroller:</p>
  
  <pre><code>&lt;?php
  
  use Symfony\Component\ClassLoader\ApcClassLoader;
  use Symfony\Component\HttpFoundation\Request;
  
  require_once __DIR__.'/../app/bootstrap.php.cache';
  
  // Use APC for autoloading to improve performance.
  // Change 'sf2' to a unique prefix in order to prevent cache key    conflicts
  // with other applications also using APC.
  $loader = new ApcClassLoader('app_1', $loader);
  $loader-&gt;register(true);
  
  require_once __DIR__.'/../app/AppKernel.php';
  
  $kernel = new AppKernel('prod', false);
  $kernel-&gt;loadClassCache();
  
  // wrap the default AppKernel with the AppCache one
  require_once __DIR__.'/../app/AppCache.php';
  $kernel = new AppCache($kernel);
  
  Request::enableHttpMethodParameterOverride();
  $request = Request::createFromGlobals();
  $response = $kernel-&gt;handle($request);
  $response-&gt;send();
  $kernel-&gt;terminate($request, $response);
  </code></pre>
  
  <p>-- Clarify --
  Just to clarify, the code above refers the APC cache for the classes, this works as above and is fine. what we can't seem to change is the default namespaces for the doctrine ORM APC entries, these seem to be fixed and thus clash with each running instance.</p>
  
  <p>-- Update --</p>
  
  <p>After trying Zerrvox's suggestion of setting the namespace, it didn't seem to work, the main caches are still using the default namespaces. It wouldn't be so bad if they were randomly generated when the cache was cleared, but the default namespaces are the same on both my vagrant box and the live box and the caches were generated on the separate boxes, so its obviously not random.</p>
  
  <p>These methods in the appProdProjectContainer class in the cache still refer to the default namespaces.</p>
  
  <pre><code>protected function getDoctrineCache_Providers_Doctrine_Orm_DefaultMetadataCacheService()
  {
  ....        
  $instance-&gt;setNamespace('sf2orm_default_9e755ef08ba52b507455ecd06d0a648985c9593b15aca1522b4725acaaf77ce6');
          return $instance;
  }
  
  // Same for...
  
  protected function getDoctrineCache_Providers_Doctrine_Orm_DefaultQueryCacheService   
  
  protected function getDoctrineCache_Providers_Doctrine_Orm_DefaultResultCacheService
  </code></pre>