Symfony 5 Cache Cheatsheet

Automatic

use Symfony\Contracts\Cache\CacheInterface;

public function baz(CacheInterface $cache) {

        return $cache->get($key, function() {
            return "Cached Data";
        });
}

Manual

use Symfony\Contracts\Cache\CacheInterface;

public function baz(CacheInterface $cache) {

    $item = $cache->getItem('foo');

    if ($item->isHit()) {
        dump('hit');
        dump($item->get());

    } else {
        $item->set('blahuuuu');
        $cache->save($item);
        sleep(3);

        dump('miss');
    }
}