PHP Performans Neden Önemli?
Sayfa yüklenme hızı hem kullanıcı deneyimini hem de SEO sıralamanızı doğrudan etkiler. Google, 3 saniyeden uzun süren siteleri cezalandırır. PHP optimizasyonu ile yanıt sürenizi %80'e kadar düşürebilirsiniz.
1. OPcache Yapılandırması
; php.ini OPcache ayarları
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=0
2. Redis Cache Entegrasyonu
class Cache {
private $redis;
public function __construct() {
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
}
public function get($key) {
$data = $this->redis->get($key);
return $data ? json_decode($data, true) : null;
}
public function set($key, $data, $ttl = 3600) {
$this->redis->setex(
$key, $ttl, json_encode($data)
);
}
public function remember($key, $ttl, $callback) {
$cached = $this->get($key);
if ($cached) return $cached;
$data = call_user_func($callback);
$this->set($key, $data, $ttl);
return $data;
}
}
3. Veritabanı Sorgu Optimizasyonu
$cache = new Cache();
$products = $cache->remember('products_list', 1800,
function() use ($db) {
$stmt = $db->query(
'SELECT * FROM products WHERE active = 1
ORDER BY sort_order ASC'
);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
);