The Alternative PHP Cache (APC) is a PHP extension that provides opcode caching and object caching. When used with the Symfony PHP framework, APC can significantly improve performance by reducing script compilation overhead and speeding up class loading.

 

Why Use APC with Symfony?

  • Faster execution: Stores precompiled PHP bytecode in memory.

  • Reduced overhead: Avoids recompiling scripts on each request.

  • Improved scalability: Handles higher traffic with fewer resources.

  • Better performance for autoloading: Symfony’s class loader can leverage APC for caching.

 

Steps to Optimize Symfony with APC

Step 1: Verify APC Installation

  1. Create a phpinfo() file in your web root:

    <?php phpinfo(); ?>
    
  2. Visit it in your browser (http://yourdomain.com/info.php).

  3. Search for APC in the output.

    • If APC is enabled, you’ll see version details.

    • If not, enable APC via cPanel’s PHP Selector or install it manually (for unmanaged servers).

 

Step 2: Use Symfony’s ApcClassLoader

Symfony provides an ApcClassLoader to cache class lookups in APC.

Example usage in app/autoload.php:

use Symfony\Component\ClassLoader\ApcClassLoader;

$loader = require __DIR__.'/../vendor/autoload.php';
$apcLoader = new ApcClassLoader('namespace_prefix', $loader);
$apcLoader->register(true);
  • Replace 'namespace_prefix' with a unique string for your application.

  • This ensures Symfony uses APC to cache class loading operations.

 

Step 3: Configure APC Settings

In your php.ini or .user.ini, adjust APC directives for optimal performance:

apc.enabled = 1
apc.shm_size = 128M
apc.ttl = 7200
apc.user_ttl = 7200
apc.gc_ttl = 3600
 
  • apc.shm_size: Shared memory size for caching.

  • apc.ttl: Time‑to‑live for cached entries.

  • Adjust values based on your application’s size and traffic.

 

Step 4: Test and Monitor

  • Use Symfony’s debug toolbar to measure performance improvements.

  • Monitor APC usage with the apc.php status script (bundled with APC).

  • Adjust settings as needed for memory and cache hit rates.

Important Notes

  • APC works only with PHP 5.4 and earlier.

  • For PHP 5.5 and later, use OPcache, which is built into PHP.

  • Symfony’s ApcClassLoader is deprecated in newer versions; use OPcache for modern deployments.

Răspunsul a fost util? 0 utilizatori au considerat informația utilă (0 Voturi)

Powered by WHMCompleteSolution