2017-01-16 127 views
1

嗨有辦法清除所有來自symfony緩存組件的緩存數據?部署後Symfony清除緩存組件

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

$cacheIsEmpty = $cache->clear(); 

和命令:

bin/console cache:clear 

保持這種高速緩存不變

我lookig控制檯命令巫婆我可以在每個部署中調用* .sh腳本。

EDIT(示例):

默認輸入選項:

$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,作曲家安裝,bin /控制檯緩存:清除...),那麼服務器上的新版本仍然有效的緩存(1天),並從中獲取數據...

+0

爲什麼./bin/console緩存:清除不夠? – sensorario

+0

問題是更新 - 示例和解釋。 –

回答

1

你可以做任何服務,你想實現Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface由symfony中使用本地緩存:明確

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(); 
    } 
} 

配置由標籤完成kernel.cache_clearer

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

編輯:精度有關高速緩存作爲一個服務:

緩存服務可以像

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

來定義,或者如果要指定一個命名空間和TTL

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

所以你應該不是使用

$cache = new FilesystemAdapter(); 

但與服務注入

$cache = $this->get('my_app_cache'); 
+0

嗨THX回答。我必須添加'使用Symfony \ Component \ Cache \ Adapter \ AdapterInterface;'並在參數中給出'@ cache.app'。但它不起作用 - 沒有錯誤,並且在bin/console cache:clear後仍然有數據從有效緩存中加載 –

+0

如果在clear()方法中放置斷點或var_dump(「called」); exit;它被稱爲? – goto

+0

在控制檯我看到'叫'所以是的 –

0

在結束 - 足以編輯只services.yml並添加:

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

我在symfony的新的,所以我希望這是正確的解決方案。