Skip to content

Optimizing Symfony using APC

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.

  • 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.
  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).

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.

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.
  • 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.