Performance

Contents
Cache pools
OPcache on
Query counts
Measure first

Cache pools

Cache the slow answers and compute them once. The pool stores the result under a key, and the closure rebuilds it only when the key is missing or expired. Reports, menus, and counters are typical first candidates.

use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; class ReportBuilder { public function __construct(private CacheInterface $cache) { } public function monthly(string $month): array { return $this->cache->get("report_" . $month, function (ItemInterface $item): array { $item->expiresAfter(3600); return ["month" => "todo", "total" => 0]; }); } }

OPcache on

In production PHP must not recompile every file on each request. OPcache keeps the compiled code in memory, which is the largest free speedup available, and it needs only two settings to start.

opcache.enable=1, opcache.validate_timestamps=0

PHP serves compiled code from memory, no recompile per request

Query counts

Most slow pages are slow database loops: one query for the list plus one query per row. Fetch the relation in the same query with a join, then compare the query count before and after in the debug toolbar.

$posts = $repo->createQueryBuilder("p") ->leftJoin("p.comments", "c") ->addSelect("c") ->getQuery() ->getResult();

Measure first

Optimize only what you measured. The dev toolbar shows page time and query count on every page, so write both numbers down, change one thing, and compare. One fix at a time beats a full rewrite.

Before: 240 ms, 35 queries

After join fetch: 60 ms, 2 queries

Next: Capstone: blog

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: