F
F
ff0xff2019-06-04 09:36:34
symfony
ff0xff, 2019-06-04 09:36:34

How to set FilesystemAdapter to work in the default cache folder in symfony 4.3?

Good day, for caching data in symfony 4 I use the FilesystemAdapter
By default I use it quite simply But when I save the cache it is saved in the /tmp/symfony-cache/ folder I don’t like this state of affairs, I want it to be saved there where is the main cache ./var/cache Of course, I looked at the FilesystemAdapter constructor
$cachePool = new FilesystemAdapter();


public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null, MarshallerInterface $marshaller = null)

It is clear that I must pass the correct $directory, but there is one caveat.
symfony puts its cache in different folders depending on the dev or prod environment.
And to that husband, I want the cache to be cleared when the cache: clear command is executed.
Tell me how you can implement this:
  • The cache should go to different folders depending on the environment.
  • Should be forced to reset when cache:clear is executed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OxCom, 2019-06-04
@ff0xff

SF4 uses the file cache by default (see framework.cache ). In this case, you need to use DI in the class constructor or controller method where this cache is needed.

public function __construct(AdapterInterface $cache) { /* ... */ }

If you have a cache other than the file system by default, then use DI and Autowire: in the framework.cache.pools configuration, describe your adapter and use it with your service or whatever you have.
# config/packages/framework.yaml
framework:
  # ...
  cache:
    pools: 
      cache.mycache: 
        adapter: cache.adapter.filesystem
        # other options

Next, we describe the service and pass the cache to it through arguments or take it from the container
$adapter = $container->get('app.cache.mycache');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question