[英]Symfony2 - How to set Doctrine APC cache prefix with multiple apps
更新:请参阅帖子的底部 (当我们使用SF 2.3时,这是在原始时发布的,我们现在在2.7.0上)
我们已经在我们的服务器上运行了多个Symfony应用程序,但由于所有主题ORM缓存条目都以“sf2”为前缀,我们似乎在各个站点中获得缓存污染。
从最近30分钟的花费开始,似乎没有一个简单的解决方案。
第一个建议使用ProjectConfiguration.class.php - 但这似乎是Symfony 1的事情。
下一个可能的解决方案是使用ApcUniversalClassLoader类(根据SO上的这个问题: 使用APC Cache的多个Symfony2站点 )。
问题是我们的设置使用composer进行自动加载,因此我们不能简单地按照该示例使用代码。
APC的类缓存方面很好,您可以在前端控制器中设置密钥,但它不会设置doctrine缓存的密钥。
有没有人有任何想法,因为目前我们不得不禁用APC作为除了第一个应用程序之外的所有学说。
Frontcontroller:
<?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建议设置命名空间后,它似乎没有用,主缓存仍在使用默认命名空间。 如果在清除缓存时随机生成它们就不会那么糟糕,但是我的流浪盒和活动框上的默认命名空间是相同的,并且缓存是在单独的框上生成的,因此它显然不是随机的。
缓存中的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 Annotations指定是否在讨论doctrine ORM缓存或注释缓存。
无论如何,您可以创建自己的APC缓存服务,可以通过在Symfony中定义服务来使用它(yml)
app.doctrine.apc_cache:
class: Doctrine\Common\Cache\ApcCache
calls:
- [setNamespace, ["app_namespace"]]
- 更新 -
您是否将定义的服务指定为doctrine的元数据缓存。 请参阅Doctrine软件包配置的配置参考http://symfony.com/doc/current/reference/configuration/doctrine.html#caching-drivers
doctrine:
orm:
metadata_cache_driver:
type: service
id: app.doctrine.apc_cache
它适用于我的情况(Symfony 2.8 - 3.4):
doctrine_cache:
providers:
my_redis:
type: redis
namespace: "%prefix%"
aliases:
- redis_cache
其中“namespace”是在parameters.yml中设置的给定app的前缀
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.