Symfony app / console cache:clear --env = prod和cache(APC,Memcached等)?

时间:2014-06-22 17:33:42

标签: symfony doctrine-orm symfony-2.3

我正在使用Memcached服务器(以及memcache PHP扩展)来缓存验证器元数据和Doctrine metatada / result / query缓存驱动程序。

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

我的问题是,命令是否:

php app/console cache:clear --env=prod --no-debug

自动清除所有类型的缓存(包括memcache)?

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

enter image description here

我的配置,其中%prod_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]

7 个答案:

答案 0 :(得分:14)

<强>缓存:明确

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

主义命令

要清除Doctrine缓存,请使用:

  • doctrine:cache:clear-metadata --env=prod
  • doctrine:cache:clear-query --env=prod
  • doctrine:cache:clear-result --env=prod

其他缓存

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

但是查看配置,您可以使用单个缓存存储来存储3个Doctrine缓存和Symfony Validator缓存。所以只调用doctrine:cache:clear-*中的一个应该清除所有内容。

非主题

当使用像APC,OPcache和其他人这样的缓存系统时,您必须了解当您从命令行运行PHP脚本时,使用不同的内存空间然后从运行PHP脚本时网络服务器。

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

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

答案 1 :(得分:5)

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

  

根据具体情况,您可能需要做很多其他事情   你的设置:           [...]清除您的APC缓存

请参阅:http://symfony.com/doc/current/cookbook/deployment/tools.html#e-other-things

答案 2 :(得分:3)

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

echo 'flush_all' | nc localhost 11211

但我想你已经知道了。

答案 3 :(得分:2)

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

Memcache:https://github.com/LeaseWeb/LswMemcacheBundle

APC:https://github.com/ornicar/ApcBundle

答案 4 :(得分:1)

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

这是确切的功能(Symfony 2.4)

    protected function execute(InputInterface $input, OutputInterface $output)
{
    $realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
    $oldCacheDir  = $realCacheDir.'_old';
    $filesystem   = $this->getContainer()->get('filesystem');

    if (!is_writable($realCacheDir)) {
        throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
    }

    if ($filesystem->exists($oldCacheDir)) {
        $filesystem->remove($oldCacheDir);
    }

    $kernel = $this->getContainer()->get('kernel');
    $output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
    $this->getContainer()->get('cache_clearer')->clear($realCacheDir);

    if ($input->getOption('no-warmup')) {
        $filesystem->rename($realCacheDir, $oldCacheDir);
    } else {
        // the warmup cache dir name must have the same length than the real one
        // to avoid the many problems in serialized resources files
        $warmupDir = substr($realCacheDir, 0, -1).'_';

        if ($filesystem->exists($warmupDir)) {
            $filesystem->remove($warmupDir);
        }

        $this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));

        $filesystem->rename($realCacheDir, $oldCacheDir);
        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
            sleep(1);  // workaround for Windows PHP rename bug
        }
        $filesystem->rename($warmupDir, $realCacheDir);
    }

    $filesystem->remove($oldCacheDir);
}

答案 5 :(得分:1)

如果您使用像APC这样的缓存,它为控制台和Web服务器使用不同的内存空间,那么您就没有其他原因可以在手动清除缓存的地方进行操作。

对于memcache,memcached等,你可以通过在运行doctrine cache clear命令的kernel.cache_clearer事件上创建一个监听器来挂钩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>

答案 6 :(得分:-1)

试试这个:

php app/console memcached:clear --clearAll default
相关问题