【问题标题】:How to flush APC class loader cache?如何刷新 APC 类加载器缓存?
【发布时间】:2013-01-31 08:05:24
【问题描述】:

Performance Symfony book 提到在某些类移动时需要刷新 APC 缓存,而这确实需要。

但是,我不知道如何清除自动装载机的 APC 缓存。我尝试使用 PHP apc_clear_cache() 函数,但没有帮助。

如何清除这个 APC 缓存?

【问题讨论】:

  • apc_clear_cache() 必须运行良好。您是否已经检查过您的问题是否与 symfony 缓存无关?
  • 谢谢,我会再次尝试apc_clear_cache,我可能没有使用正确的字符串。我也清除了 Symfony 缓存(以及所有 Composer 生成的自动加载器),但没有成功。

标签: symfony apc


【解决方案1】:

正如 Mauro 所说,apc_clear_cache 也可以带一个参数来清除不同类型的 apc 缓存:

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

另请参阅this related post on SO

还有ApcBundle 添加了一个 Symfony apc:clear 命令。

【讨论】:

  • 谢谢,这正是我想要的。
  • 有关信息,必须在 Symfony2 应用程序中执行上述行。在app.php 中临时添加它们就可以了。
【解决方案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_ADMIN, ROLE_ADMIN") 注释,这将保护您的 apc 缓存 url 免受未经授权的访问。

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 2016-03-25
    • 2018-02-16
    • 2019-10-20
    • 2017-01-06
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多