更新:见帖子底部 (这最初是在我们使用 SF 2.3 时发布的,我们现在使用的是 2.7.0)
我们的服务器上运行了多个 Symfony 应用程序,但由于所有 ORM 缓存条目都以“sf2”为前缀,我们似乎在整个站点上都遇到了缓存污染。
在过去的 30 分钟里四处寻找,似乎没有一个简单的解决方案。
第一个建议使用 ProjectConfiguration.class.php - 但这似乎是 Symfony 1 的事情。
下一个可能的解决方案是使用 ApcUniversalClassLoader 类(根据 SO 上的这个问题:Multiple Symfony2 Sites using APC Cache)。
问题是我们的设置使用 composer 来自动加载,所以我们不能简单地按照那个例子使用代码。
APC的类缓存端没问题,可以在前端 Controller 设置key,但是它没有为doctrine cache设置key。
有没有人有任何想法,因为目前我们不得不在除第一个应用程序之外的所有应用程序上禁用 APC。
前 Controller :
<?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->register(true);
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('prod', false);
$kernel->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->handle($request);
$response->send();
$kernel->terminate($request, $response);
-- 澄清-- 只是为了澄清,上面的代码引用了类的 APC 缓存,这与上面的一样工作并且很好。我们似乎无法更改的是 Doctrine ORM APC 条目的默认命名空间,这些似乎是固定的,因此与每个正在运行的实例发生冲突。
-- 更新--
在尝试了 Zerrvox 的设置命名空间的建议后,似乎没有用,主要缓存仍在使用默认命名空间。如果它们是在清除缓存时随机生成的,那就不会那么糟糕了,但是我的 vagrant box 和 live box 上的默认命名空间是相同的,并且缓存是在单独的 box 上生成的,所以它显然不是随机的。
缓存中的 appProdProjectContainer 类中的这些方法仍然引用默认命名空间。
protected function getDoctrineCache_Providers_Doctrine_Orm_DefaultMetadataCacheService()
{
....
$instance->setNamespace('sf2orm_default_9e755ef08ba52b507455ecd06d0a648985c9593b15aca1522b4725acaaf77ce6');
return $instance;
}
// Same for...
protected function getDoctrineCache_Providers_Doctrine_Orm_DefaultQueryCacheService
protected function getDoctrineCache_Providers_Doctrine_Orm_DefaultResultCacheService
最佳答案
您没有具体说明您是在谈论 Doctrine ORM 缓存还是使用 Doctrine Annotations 的注释缓存。
无论如何你可以创建你自己的 APC 缓存服务,它可以通过在 Symfony 中像这样定义一个服务来用于两者(yml)
app.doctrine.apc_cache:
class: Doctrine\Common\Cache\ApcCache
calls:
- [setNamespace, ["app_namespace"]]
-- 更新--
您是否将定义的服务指定为 doctrine 的元数据缓存。请参阅 Doctrine bundle 配置的配置引用 http://symfony.com/doc/current/reference/configuration/doctrine.html#caching-drivers
doctrine:
orm:
metadata_cache_driver:
type: service
id: app.doctrine.apc_cache
关于php - Symfony2 - 如何为多个应用程序设置 Doctrine APC 缓存前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28275201/