部署

时间:2017-01-16 15:54:41

标签: caching deployment components symfony

您可以通过symfony缓存组件清除所有缓存数据吗?

底部的http://symfony.com/doc/current/components/cache/cache_pools.html是:(我需要控制台命令)

$cacheIsEmpty = $cache->clear();

和命令:

bin/console cache:clear

保持此缓存不变

我正在寻找控制台命令,我可以在每次部署时调用* .sh脚本。

编辑(示例):

默认输入选项:

 $cache = new FilesystemAdapter();
 $defaultInputOptions = $cache->getItem('mainFilter.defaultInputOptions');

 if (!$defaultInputOptions->isHit()) {
      // collect data, format etc.
      $expiration = new \DateInterval('P1D');
      $defaultInputOptions->expiresAfter($expiration);
      $cache->save($defaultInputOptions);
 } else {
      return $defaultInputOptions->get();
 }

但如果我改变了收集数据,格式化等内容的话。'在我的机器上,然后进行部署(git pull,composer install,bin / console cache:clear ...)然后服务器上的新版本仍然有效缓存(1天)并从中获取数据......

2 个答案:

答案 0 :(得分:1)

您可以将您想要的任何服务实现为{strong}来使用symfony 原生缓存:清除

Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface

Config由标签 kernel.cache_clearer

完成
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;

final class SomeCacheClearer implements CacheClearerInterface
{
    private $cache;

    public function __construct(AdapterInterface $filesystemAdapter)
    {
        $this->cache = $filesystemAdapter;
    }

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

编辑:关于缓存作为服务的精度:

您的缓存服务可以像那样定义

cache_provider.clearer:
    class: AppBundle\Path\SomeCacheClearer
    arguments:
        - 'my_app_cache'
    tags:
        - { name: kernel.cache_clearer }

或者如果要指定命名空间和ttl

my_app_cache:
    class: Symfony\Component\Cache\Adapter\FilesystemAdapter

所以你应该使用

my_app_cache:
    class: Symfony\Component\Cache\Adapter\FilesystemAdapter
    arguments:
        - 'my_cache_namespace'
        - 30 #cache ttl  (in seconds)

但是服务注入

$cache = new FilesystemAdapter();

答案 1 :(得分:0)

最后 - 只需编辑services.yml并添加:

appbundle.cachecomponent.clearall:
    class: Symfony\Component\Cache\Adapter\FilesystemAdapter
    tags:
        - { name: kernel.cache_clearer }

我是symfony的新手,所以我希望这是正确的解决方案。