1

The Performance Symfony book mentions the need to flush the APC cache when some classes have moved and this is indeed needed.

However, I don't find how to clear the APC cache for autoloaders. I tried with the PHP apc_clear_cache() function, but it didn't help.

How to clear this APC cache?

2
  • apc_clear_cache(<string>) must works well. Do you already check if your problem is not with the symfony cache?
    – Mauro
    Feb 15, 2013 at 12:17
  • Thanks, I will try with apc_clear_cache again, I may have not used the correct string. I cleared the Symfony cache as well (and all Composer generated autoloaders), but with no success. Feb 15, 2013 at 13:31

2 Answers 2

5

As mentioned by Mauro apc_clear_cache can also take an argument to clear different types of apc caches:

  apc_clear_cache();
  apc_clear_cache('user');
  apc_clear_cache('opcode');

Please also see this related post on SO.

And there is also ApcBundle which adds a Symfony apc:clear command.

1
  • For information, the mentioned lines have to be executed in the Symfony2 application. Adding them temporarily in app.php does the trick. Jul 4, 2013 at 15:19
2

Just create one simple controller ApcController as below

<?php

namespace Rm\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use JMS\SecurityExtraBundle\Annotation\Secure;

/**
 * apc cache clear controller
 */
class ApcController extends Controller
{

    /**
     * clear action
     *
     * @Route("/cc", name="rm_demo_apc_cache_clear")
     *
     * @Secure(roles="ROLE_SUPER_ADMIN, ROLE_ADMIN")
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     */
    public function cacheClearAction(Request $request)
    {

        $message = "";

        if (function_exists('apc_clear_cache') 
                && version_compare(PHP_VERSION, '5.5.0', '>=') 
                && apc_clear_cache()) {

            $message .= ' User Cache: success';

        } elseif (function_exists('apc_clear_cache') 
                && version_compare(PHP_VERSION, '5.5.0', '<') 
                && apc_clear_cache('user')) {

            $message .= ' User Cache: success';

        } else {

            $success = false;
            $message .= ' User Cache: failure';

        }

        if (function_exists('opcache_reset') && opcache_reset()) {

            $message .= ' Opcode Cache: success';

        } elseif (function_exists('apc_clear_cache') 
                && version_compare(PHP_VERSION, '5.5.0', '<') 
                && apc_clear_cache('opcode')) {

            $message .= ' Opcode Cache: success';

        } else {
            $success = false;
            $message .= ' Opcode Cache: failure';
        }

        $this->get('session')->getFlashBag()
                            ->add('success', $message);

        // redirect
        $url = $this->container
                ->get('router')
                ->generate('sonata_admin_dashboard');

        return $this->redirect($url);
    }

}

Then import controller routes to your routing.yml

#src/Rm/DemoBundle/Resources/config/routing.yml
apc:
    resource: "@RmDemoBundle/Controller/ApcController.php"
    type:     annotation
    prefix:   /apc

Now you can clear apc cache using below url:

http://yourdomain/apc/cc

NOTE : @Secure(roles="ROLE_SUPER_ADMIN, ROLE_ADMIN") annotation, this will protect you apc cache url from unauthorized access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.