2013-02-15 248 views
1

Performance Symfony book提到当某些类已经移动并且确实需要时,需要刷新APC缓存。如何刷新APC类加载器缓存?

但是,我没有找到如何清除自动加载器的APC缓存。我尝试使用PHP apc_clear_cache()函数,但它没有帮助。

如何清除此APC缓存?

+0

apc_clear_cache()必须正常工作。你已经检查你的问题是不是与symfony缓存? – Mauro 2013-02-15 12:17:12

+0

谢谢,我会再次尝试'apc_clear_cache',我可能没有使用正确的字符串。我也清除了Symfony缓存(以及所有Composer生成的自动加载器),但没有成功。 – 2013-02-15 13:31:29

回答

5

正如毛罗提到apc_clear_cache也可以采取一个参数来清除不同类型的APC缓存:

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

另请参阅this related post on SO

另外还有ApcBundle,它增加了一个Symfony apc:clear命令。

+0

谢谢,这正是我正在寻找的。 – 2013-05-14 09:36:05

+0

有关信息,上述行必须在Symfony2应用程序中执行。在'app.php'中临时添加它们是有用的。 – 2013-07-04 15:19:11

2

只需创建一个简单的控制器ApcController如下

<?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); 
    } 

} 

然后导入控制器路由到您的的routing.yml

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

现在,您可以通过以下网址清除缓存APC:

http://yourdomain/apc/cc 

注意:@Secure(roles =“ROLE_SUPER_AD MIN,ROLE_ADMIN“)注释,这将保护您apc缓存url免受未经授权的访问。