当我想在我的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;
}
}
答案 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}}');
}