Lajdák Marek
Lajdák Marek

Reputation: 3069

Symfony clear cache component after deploy

Hi is there way to clear ALL cached data from symfony cache component?

Here http://symfony.com/doc/current/components/cache/cache_pools.html on bottom is: (i need console command)

$cacheIsEmpty = $cache->clear();

and command:

bin/console cache:clear

keeps this cache untouched

I am lookig for console command witch i can call in *.sh script every deploy.

EDIT (example):

Default input options:

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

But if i change something in 'collect data, format etc.' on my machine and after that make deploy (git pull, composer install, bin/console cache:clear...) then new version on server has still valid cache (1 day) and take data from it...

Upvotes: 3

Views: 1693

Answers (2)

Lajdák Marek
Lajdák Marek

Reputation: 3069

At the end - suffices edit only services.yml and add:

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

I am new in symfony so i hope that is right solution.

Upvotes: 1

goto
goto

Reputation: 8162

You can make any service you want implement the Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface to be used by symfony native cache:clear

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

Config is done by the tag kernel.cache_clearer

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

EDIT: precision about cache as a service:

Your cache service could be defined like that

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

Or if you want to specify a namespace and ttl

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

So you should not use

$cache = new FilesystemAdapter();

But with service injection

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

Upvotes: 2

Related Questions