Кеш

← Кеш

Ashterix

docs(articles): Magento and Symfony examples side by side, request lifecycle as a Mermaid diagram

uk/articles/magento-to-symfony/infrastructure/cache.md +6 −1

Кеш

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']);

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() з callback захищає від stampede: обчислення запускає лише один процес.
  • Пули описують у framework.cache.pools: окремий Redis для сесій, окремий для даних.
  • cache:clear чистить лише системний кеш (контейнер, маршрути, метадані). Дані в пулах чистить cache:pool:clear cache.app.
  • Повної сторінкової кеш-системи немає. Є HTTP-кеш: #[Cache(public: true, maxage: 600)] на дії, HttpCache як PHP reverse proxy, ESI для фрагментів. Інвалідація за тегами у Varnish, як X-Magento-Tags, — через FOSHttpCacheBundle.