Yii2 - 从控制台使用 APC 缓存

Yii2 - Use APC cache from console

我正在尝试使用控制台命令将数据添加到 APC 缓存,但到目前为止没有任何成功。

当我 运行 将此代码作为标准操作 (controller/action) 时,此代码完美运行:

    public function actionUpdateExchangeRate()
    {

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL            => 'https://openexchangerates.org/api/latest.json?app_id=xxxxxxx',
            CURLOPT_USERAGENT      => 'Exchange Rates'
        ));

        $json = curl_exec($curl);

        curl_close($curl);

        $rates = json_decode($json);

        Yii::$app->cache->set('rates', $rates->rates);

    }

我用相同的代码创建了一个命令,当我尝试设置缓存时,没有任何反应。

var_dump('<pre>', Yii::$app->cache->set('rates', $rates->rates), '</pre>');die;

转储 true 当 运行 作为 controler/actionfalse 当 运行 来自命令时。

console.php 中,我将此配置添加到组件中(在 web.php 中是相同的):

    'cache' => [
        'class'     => 'yii\caching\ApcCache',
        'keyPrefix' => 'test',
        'useApcu'   => true
    ],

PHP 版本为:

PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr  5 2018 08:53:57) ( NTS )

知道我做错了什么吗?

确保您已在 php.ini 中启用 apc.enable_cli

但是使用 ApcCache 控制台没有多大意义。 APCu 缓存是每个进程的,所以它会在命令结束后被删除,控制台命令不会与 Web 请求共享缓存。

Mostly for testing and debugging. Setting this enables APC for the CLI version of PHP. Under normal circumstances, it is not ideal to create, populate and destroy the APC cache on every CLI request, but for various test scenarios it is useful to be able to enable APC for the CLI version of PHP easily.

https://secure.php.net/manual/en/apcu.configuration.php#ini.apcu.enable-cli