如何在yii2 dbcache中使用('cache')创建表?

时间:2016-12-12 15:08:34

标签: php yii2

当我想在我的yii2代码中使用yii\caching\DbCache时,我得到:

  

错误:表“缓存”不存在。

如何创建此表?

config / web.php:

'cache' => [
        'class' => 'yii\caching\DbCache',
    ],

控制器代码:

$cache = Yii::$app->cache;
    $duration = 30;

    if($currency == 'USD')
    {
        // try retrieving $data from cache
        $data = $cache->get('getCurrencyUSD');
        if($data === false)
        {
            $url = 'wsdl file address ...';
            $client = new SoapClient($url);
            $data = $client->getCurrency('USD');
            $cache->set('getCurrencyUSD', $data, $duration);
            return $data;
        }
    }

1 个答案:

答案 0 :(得分:1)

创建新迁移并添加following example

public function up()
{
    $this->createTable('{{%cache}}', [
        'id' => $this->char(128)->notNull(),
        'expire' => $this->integer()->null(),
        'data' => 'BLOB',
    ]);

    $this->addPrimaryKey('pk-cache', '{{%cache}}', 'id');
}

public function down()
{
   $this->dropTable('{{%cache}}');
}