Cache

← Cache

Ashterix

feat(seo): og:image size for front matter images; short titles and explicit descriptions for From Magento to Symfony

en/articles/magento-to-symfony/infrastructure/cache.md +2 −1

Cache

Instead of Magento cache types, Symfony gives you tagged cache pools through Cache Contracts and standard HTTP caching. There is no full page cache out of the box.

Magento · CacheInterface

$data = $this->cache->load('faq_popular');
if ($data === false) {
    $data = $this->serializer->serialize(
        $this->repository->getPopular(5)
    );
    $this->cache->save($data, 'faq_popular', ['faq'], 3600);
}

$this->cache->clean(['faq']);

Symfony · Cache Contracts

public function __construct(
    private TagAwareCacheInterface $cache,
) {}

$popular = $this->cache->get('faq_popular', function (ItemInterface $item) {
    $item->expiresAfter(3600);
    $item->tag(['faq']);
    return $this->questions->findPopularIds(5);
});

$this->cache->invalidateTags(['faq']);
  • get() with a callback protects against stampede: only one process runs the computation.
  • Pools are defined in framework.cache.pools: a separate Redis for sessions, a separate one for data.
  • cache:clear clears only the system cache (container, routes, metadata). Data in pools is cleared with cache:pool:clear cache.app.
  • There is no full page cache system. There is HTTP cache: #[Cache(public: true, maxage: 600)] on an action, HttpCache as a PHP reverse proxy, ESI for fragments. Tag-based invalidation in Varnish, like X-Magento-Tags, is done via FOSHttpCacheBundle.