Performance is not a luxury -- it directly impacts business results. Slow pages cost visitors and therefore revenue. Here are my proven optimizations for PHP applications.
Configuring OPcache Properly
OPcache is the simplest and most effective performance lever. These are the settings I recommend for production systems:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0
opcache.save_comments=1
opcache.enable_file_override=1
Important: validate_timestamps=0 means that PHP files are only reloaded after a service restart. This is the desired behavior on production systems.
Optimizing Database Queries
The most common cause of slow applications is inefficient database queries. Typical mistakes:
// Schlecht: N+1 Problem
$orders = $orderRepository->findAll();
foreach ($orders as $order) {
// Löst für jede Order eine weitere Query aus
$customer = $order->getCustomer();
echo $customer->getName();
}
// Besser: Eager Loading mit JOIN
$qb = $entityManager->createQueryBuilder();
$orders = $qb->select('o', 'c')
->from(Order::class, 'o')
->join('o.customer', 'c')
->getQuery()
->getResult();
Caching Strategies
A multi-level caching approach significantly speeds up your application:
<?php
declare(strict_types=1);
namespace App\Service;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class ProductService
{
public function __construct(
private readonly CacheInterface $cache,
private readonly ProductRepository $repository,
) {
}
public function getFeaturedProducts(): array
{
return $this->cache->get('featured_products', function (ItemInterface $item) {
$item->expiresAfter(3600);
return $this->repository->findFeatured();
});
}
}
Preloading in PHP 7.4+
PHP 7.4 introduced preloading, which allows you to load frequently used classes into memory at server startup:
<?php
// preload.php
require __DIR__ . '/vendor/autoload.php';
// Framework-Klassen preloaden
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(__DIR__ . '/vendor/symfony')
);
foreach ($files as $file) {
if ($file->getExtension() === 'php') {
opcache_compile_file($file->getPathname());
}
}
Conclusion
Performance optimization starts with measurement. Tools like Blackfire, Xdebug Profiler, or simply Symfony's built-in Profiler toolbar help identify the actual bottlenecks. Optimize with purpose -- not on a hunch.
Kommentare
Kommentare werden von Remark42 bereitgestellt. Beim Laden werden Daten an unseren Kommentar-Server übertragen.