Symfony应用程序/控制台缓存:清除--env=prod和缓存(APC、Memcached等)?

Symfony应用程序/控制台缓存:清除--env=prod和缓存(APC、Memcached等)?,symfony,doctrine-orm,symfony-2.3,Symfony,Doctrine Orm,Symfony 2.3,我使用Memcached服务器(以及memcachephp扩展)缓存验证器元数据和metatada/result/query缓存驱动程序 一切都按预期工作,与文件系统缓存相比速度更快 我的问题是,命令是否: php app/console cache:clear --env=prod --no-debug 是否自动清除所有类型的缓存(包括memcache) 运行此命令并检查服务器统计信息后,项计数和缓存占用始终相同: 我的配置,其中%prod\u cache%参数实际上是字符串memcach

我使用Memcached服务器(以及memcachephp扩展)缓存验证器元数据和metatada/result/query缓存驱动程序

一切都按预期工作,与文件系统缓存相比速度更快

我的问题是,命令是否:

php app/console cache:clear --env=prod --no-debug
是否自动清除所有类型的缓存(包括memcache)

运行此命令并检查服务器统计信息后,项计数和缓存占用始终相同:

我的配置,其中%prod\u cache%参数实际上是字符串
memcache

# Framework Configuration
framework:
    validation:
        cache: %prod_cache% # Matches validator cache service

# Doctrine Configuration
doctrine:
    orm:
        metadata_cache_driver:
            type: service
            id: cache_%prod_cache%
        result_cache_driver:
            type: service
            id: cache_%prod_cache%
        query_cache_driver:
            type: service
            id: cache_%prod_cache%

# DoctrineCacheBundle Configuration
doctrine_cache:
    providers:
        memcache:
            type: memcache
            alias: cache_memcache

# Services
services:
    validator.mapping.cache.memcache: # Validator cache service
        class: Symfony\Component\Validator\Mapping\Cache\DoctrineCache
        arguments: [@cache_memcache]

您必须自己从文档中清除外部缓存(SF 2.5):

根据不同的情况,你可能还需要做很多其他的事情 您的设置: […]正在清除APC缓存


请参阅:

如果选中vendor/symfony/src/symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php,您将看到symfony使用文件系统服务清除缓存。它通过删除旧目录来清除缓存

下面是确切的函数(Symfony 2.4)

受保护的函数执行(InputInterface$input,OutputinInterface$output)
{
$realCacheDir=$this->getContainer()->getParameter('kernel.cache_dir');
$oldCacheDir=$realCacheDir.“'u old';
$filesystem=$this->getContainer()->get('filesystem');
如果(!是可写的($realCacheDir)){
抛出新的\RuntimeException(sprintf('无法写入“%s”目录',$realCacheDir));
}
如果($filesystem->exists($oldCacheDir)){
$filesystem->remove($oldCacheDir);
}
$kernel=$this->getContainer()->get('kernel');
$output->writeln(sprintf('s Clearing the cache for the%s environment with debug%s',$kernel->getEnvironment(),var_export($kernel->isDebug(),true));
$this->getContainer()->get('cache\u clear')->clear($realCacheDir);
如果($input->getOption('no-warmup')){
$filesystem->rename($realCacheDir,$oldCacheDir);
}否则{
//预热缓存目录名称的长度必须与实际名称的长度相同
//避免序列化资源文件中的许多问题
$warmupDir=substr($realCacheDir,0,-1)。”;
如果($filesystem->exists($warmupDir)){
$filesystem->remove($warmupDir);
}
$this->warmup($warmupDir,$realCacheDir,!$input->getOption('no-optional-warmers');
$filesystem->rename($realCacheDir,$oldCacheDir);
if(已定义('PHP\u WINDOWS\u VERSION\u BUILD')){
sleep(1);//解决Windows PHP重命名错误的方法
}
$filesystem->rename($warmupDir,$realCacheDir);
}
$filesystem->remove($oldCacheDir);
}

此命令不会清除Memcache,因为它是一个独立的分布式内存缓存。 它只清除Symfony缓存。 要清除memcache,必须手动运行:

echo 'flush_all' | nc localhost 11211

但我想你已经知道了。缓存:清除

cache:clear
命令不会清除条令缓存。它只清除一些Symfony 2框架特定的缓存,主要是
app/cache
(或
var/cache
)目录

条令命令

要清除条令缓存,请使用:

  • 原则:缓存:清除元数据--env=prod
  • 原则:缓存:清除查询--env=prod
  • 原则:缓存:清除结果--env=prod
附加缓存

如果使用其他缓存存储,则必须自己清除它们。您可以查看
原则:cache:clear-*
命令,了解如何创建自己的命令

但从您的配置来看,您对3个条令缓存和Symfony验证器缓存使用单个缓存存储。因此,只要调用
原则中的一个:cache:clear-*
就可以清除所有内容

离题

当使用APC、OPcache等缓存系统时,您必须了解,当您从命令行运行PHP脚本时,从Web服务器运行PHP脚本时会使用不同的内存空间

换句话说:当您从命令行清除此类缓存时,Web服务器使用的缓存不会受到影响。为了清除Web服务器缓存,您需要从Web服务器本身运行缓存清除脚本


这不是memcached的问题,因为它是一个单独的程序,管理自己的内存空间。

clear:cache命令只清空文件系统中的symfony缓存,然后对其进行预热。 您可以使用一些其他捆绑包来清除其他缓存:

Memcache:

APC:

试试这个:

php app/console memcached:clear --clearAll default

如果您使用像APC这样的缓存,它为控制台和web服务器使用不同的内存空间,那么您就没有其他的原因,也没有手动清除缓存的接口

对于memcache、memcached等,您可以通过在kernel.cache\u clear事件上创建一个侦听器来钩住cache:clear命令,该事件运行两个命令cache clear

无论使用何种方法清除缓存,某些缓存类型(如memcache)都不会释放所使用的内存。相反,当下一次对该键发出get命令时,或者当内存已满且需要收回某些键时,它们将取消设置该键

class CacheClearDoctrineListener implements CacheClearerInterface
{
/**
 * @var ContainerInterface
 */
protected $container;

protected $environment;

public function setContainer(ContainerInterface $container = null)
{
    $this->container = $container;
    $this->environment = $container->get('kernel')->getEnvironment();
}

public function clear($cacheDir)
{
    $this->clearCacheMetadata();
    $this->clearCacheQuery();
}

protected function clearCacheMetadata()
{
    // clear cache metadata
    $application = new Application($this->container->get('kernel'));
    $application->setAutoExit(false);

    $output = new ConsoleOutput();
    $arguments = array(
        "command" => "doctrine:cache:clear-metadata",
        "--env" => $this->environment
    );
    $input = new ArrayInput($arguments);
    $returnCode = $application->run($input, $output);

    if ($returnCode != 0) {
        $output->writeln(json_decode(rtrim($output)));
    }
}

protected function clearCacheQuery()
{
    // clear cache query
    $application = new Application($this->container->get('kernel'));
    $application->setAutoExit(false);

    $output = new ConsoleOutput();
    $arguments = array(
        "command" => "doctrine:cache:clear-query",
        "--env" => $this->environment
    );
    $input = new ArrayInput($arguments);
    $returnCode = $application->run($input, $output);

    if ($returnCode != 0) {
        $output->writeln(json_decode(rtrim($output)));
    }
}
}
声明您的服务如下:

<service id="cache_clear_doctrine_clearer" class="%namespace_of_your_listener%">
    <call method="setContainer">
        <argument type="service" id="service_container"/>
    </call>
    <tag name="kernel.cache_clearer"  priority="254" />
</service>

Symfony/Doctrine实际上有用于此的命令,请参见不要忘记在Doctrine命令末尾添加--env=prod!
<service id="cache_clear_doctrine_clearer" class="%namespace_of_your_listener%">
    <call method="setContainer">
        <argument type="service" id="service_container"/>
    </call>
    <tag name="kernel.cache_clearer"  priority="254" />
</service>