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?

j0k
  • 22,600
  • 28
  • 79
  • 90
Michaël Perrin
  • 5,903
  • 5
  • 40
  • 65
  • apc_clear_cache() must works well. Do you already check if your problem is not with the symfony cache? – Mauro Feb 15 '13 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. – Michaël Perrin Feb 15 '13 at 13:31

2 Answers2

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.

Community
  • 1
  • 1
Fooman
  • 1,340
  • 9
  • 11
  • For information, the mentioned lines have to be executed in the Symfony2 application. Adding them temporarily in `app.php` does the trick. – Michaël Perrin Jul 04 '13 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.

Rajesh Meniya
  • 753
  • 1
  • 7
  • 17