Skip to content

Symfony 7: Cache

# Check service
bin/console debug:autowiring cache

# Check pools
symfony console cache:pool:list

# Clear pool
symfony console cache:pool:clear cache.app

# Clear cache
symfony console cache:clear

Example

use Symfony\Contracts\Cache\ItemInterface;

$httpClient = ...

$issData = $cache->get(
    'iss_location_data',
    /**
      * Anonymous function.
      * Variables declared outside have to be `use`-d (`$httpClient`)
      * Class properties can be called directly (`$this->` should work)
    */
    function (ItemInterface $item) use ($httpClient): array {
        // Time is in seconds
        $item->expiresAfter(5);
        $response = $httpClient->request('GET', 'https://api.wheretheiss.at/v1/satellites/25544');
        return $response->toArray();
    }
);

Notes