PHP OPcache संबंधित issues को debug और diagnose करने के तरीके।
त्वरित निदान: OPcache अस्थायी रूप से अक्षम करें
<?php
// Script के शीर्ष पर रखें
ini_set('opcache.enable', 0);
// या .user.ini में
opcache.enable=0
// या php.ini में
opcache.enable=0
यदि issue resolve हो जाता है, तो OPcache culprit है।
Error Reporting सक्षम करें
<?php
// Development के लिए
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Production के लिए logging
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/php_errors.log');
error_reporting(E_ALL);
OPcache Status और Configuration Check करें
<?php // OPcache status $status = opcache_get_status(); print_r($status); // Key metrics to check: echo "Enabled: " . ($status['opcache_enabled'] ? 'Yes' : 'No') . "\n"; echo "Memory used: " . round($status['memory_usage']['used_memory'] / 1024 / 1024, 2) . " MB\n"; echo "Memory free: " . round($status['memory_usage']['free_memory'] / 1024 / 1024, 2) . " MB\n"; echo "Wasted: " . round($status['memory_usage']['wasted_memory'] / 1024 / 1024, 2) . " MB\n"; echo "Cached scripts: " . $status['opcache_statistics']['num_cached_scripts'] . "\n"; echo "Hit rate: " . round($status['opcache_statistics']['opcache_hit_rate'], 2) . "%\n";
OPcache Configuration Optimization (php.ini)
; OPcache settings opcache.enable=1 opcache.enable_cli=1 opcache.memory_consumption=256 ; MB (increase if wasted memory high) opcache.interned_strings_buffer=16 ; MB opcache.max_accelerated_files=20000 ; Increase for large apps opcache.revalidate_freq=0 ; 0 = check every request (dev), >0 for prod opcache.validate_timestamps=1 ; 1 = check timestamps, 0 = never (prod) opcache.save_comments=1 ; Required for annotations opcache.fast_shutdown=1 ; Faster shutdown opcache.enable_file_override=0 ; Security opcache.optimization_level=0x7FFFBFFF ; All optimizations ; Logging opcache.error_log=/var/log/opcache.log opcache.log_verbosity_level=1 ; 1 = errors only, 2 = warnings, 3 = info
Common Issues और Solutions
| Issue | Symptoms | Solution |
|---|---|---|
| Stale cache | Code changes not reflected | Set opcache.validate_timestamps=1, opcache.revalidate_freq=0 (dev) |
| Memory exhaustion | "Cannot allocate memory" | Increase opcache.memory_consumption |
| Corrupted cache | Random crashes, segfaults | opcache_reset() or restart PHP-FPM |
| File permission issues | Permission denied errors | Check file ownership, opcache.file_cache permissions |
| Symlink issues | Deployments not picked up | Use opcache.validate_timestamps=1 or restart after deploy |
Cache Clear करने के तरीके
<?php
// Programmatically
opcache_reset(); // All caches clear
// Specific file (PHP 7.0+)
opcache_invalidate('/path/to/file.php', true); // true = force
// CLI से
php -r "opcache_reset();"
// Systemd के साथ
systemctl reload php-fpm
# या
systemctl restart php-fpm
Xdebug के साथ Advanced Debugging
<?php
// Xdebug + OPcache compatibility
// xdebug.mode=debug,develop
// xdebug.start_with_request=yes
// OPcache के साथ Xdebug काम करता है लेकिन:
// - Breakpoints optimized-out code पर नहीं लग सकते
// - Variable values optimized away हो सकते हैं
// Development के लिए OPcache disable करें:
ini_set('opcache.enable', 0);
Deployment Best Practices
# Deploy script में OPcache clear करें #!/bin/bash # 1. Deploy new code # 2. Clear OPcache php -r "opcache_reset();" # 3. या PHP-FPM reload (clears cache) systemctl reload php-fpm # Zero-downtime के लिए: atomic symlink swap + reload
Monitoring Script
<?php
// opcache_monitor.php - cron job के लिए
$status = opcache_get_status();
$hitRate = $status['opcache_statistics']['opcache_hit_rate'];
$wastedPct = ($status['memory_usage']['wasted_memory'] / $status['memory_usage']['used_memory']) * 100;
if ($hitRate < 90) {
error_log("OPcache hit rate low: {$hitRate}%");
}
if ($wastedPct > 10) {
error_log("OPcache wasted memory high: {$wastedPct}%");
// Consider restart or increase memory
}