Cache

← Cache

Ashterix

docs(articles): English translation of From Magento to Symfony

en/articles/magento-to-symfony/infrastructure/cache.md new file +36 βˆ’0

1 + # Cache
2 +
3 + **Magento** Β· CacheInterface
4 +
5 + ```php
6 + $data = $this->cache->load('faq_popular');
7 + if ($data === false) {
8 + $data = $this->serializer->serialize(
9 + $this->repository->getPopular(5)
10 + );
11 + $this->cache->save($data, 'faq_popular', ['faq'], 3600);
12 + }
13 +
14 + $this->cache->clean(['faq']);
15 + ```
16 +
17 + **Symfony** Β· Cache Contracts
18 +
19 + ```php
20 + public function __construct(
21 + private TagAwareCacheInterface $cache,
22 + ) {}
23 +
24 + $popular = $this->cache->get('faq_popular', function (ItemInterface $item) {
25 + $item->expiresAfter(3600);
26 + $item->tag(['faq']);
27 + return $this->questions->findPopularIds(5);
28 + });
29 +
30 + $this->cache->invalidateTags(['faq']);
31 + ```
32 +
33 + - `get()` with a callback protects against stampede: only one process runs the computation.
34 + - Pools are defined in `framework.cache.pools`: a separate Redis for sessions, a separate one for data.
35 + - `cache:clear` clears only the system cache (container, routes, metadata). Data in pools is cleared with `cache:pool:clear cache.app`.
36 + - 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.