Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 351
Lajdák Marek Avatar asked Mar 14 '26 10:03

Lajdák Marek


1 Answers

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');
like image 98
goto Avatar answered Mar 16 '26 07:03

goto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!