Configuring Symfony2 to use APC cache


Symfony2 support several caching systems and one of them is APC cache.

This article is going to be short …. but, everything in Symfony2 is so developer friendly that do not require some huge knowledge to accomplish …

So, first of all what you should know: Apc cache is not going to work properly unless you are executing php scripts on linux server configuration either as apache user or FastCGI and the best way to be configured (from my personal experience is to run Php as with mod_fcgid, because running php as Apache user with often insufficient permissions could lead to other problems …).

But this article is not about how to configure your web server, but how easy is to configure Symfony2 project to use already working APC cache on server.

First, locate your file autoload.php inside app folder of your project and change few lines.

You have to change default’s lines that use UniversalClassLoader to ApcUniversalClassLoader:

<!--?php require __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php'; use Symfony\Component\ClassLoader\ApcUniversalClassLoader; //use Symfony\Component\ClassLoader\UniversalClassLoader; use Doctrine\Common\Annotations\AnnotationRegistry; $loader = new ApcUniversalClassLoader('class_cache_123'); //$loader = new UniversalClassLoader(); $loader--->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    'Sensio'           => __DIR__.'/../vendor/bundles',
    'JMS'              => __DIR__.'/../vendor/bundles',
    'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
    'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
    'Doctrine\\DBAL'   => __DIR__.'/../vendor/doctrine-dbal/lib',
    'Doctrine'         => __DIR__.'/../vendor/doctrine/lib',
    'Monolog'          => __DIR__.'/../vendor/monolog/src',
    'Assetic'          => __DIR__.'/../vendor/assetic/src',
    'Metadata'         => __DIR__.'/../vendor/metadata/src',
    'Surgeworks'         => __DIR__.'/../src',
    'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
    'Twig_'            => __DIR__.'/../vendor/twig/lib',
// intl
if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
AnnotationRegistry::registerLoader(function($class) use ($loader) {
    return class_exists($class, false);
// Swiftmailer needs a special autoloader to allow
// the lazy loading of the init file (which is expensive)
require_once __DIR__.'/../vendor/swiftmailer/lib/classes/Swift.php';

After that, you can use APC cache within your Symfony2 project.
Currently, I am using it just for Doctrine2 and will explain how to cache Doctrine2 DQL results in next article …

I honestly hope that this few lines will be useful to you.


You made it all the way down here so you must enjoyed this post! You may also like:

Aug 01, 2017

Adding custom entries to admin system configuration

/ Leave a comment
Nov 03, 2016

File upload in Magento 2 store configuration

/ Leave a comment
Dec 02, 2011

Symfony2 caching Doctrine2 results

/ Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.