as you suggest,I create table cache:
CREATE TABLE cache (
id char(128) NOT NULL PRIMARY KEY,
expire int(11),
data BLOB
);
because the id field is primary key,that is the problem:
Integrity constraint violation: 1062 Duplicate entry 'key' for key 'PRIMARY'
//first time call
$cache->set('key','some data');
//the second call
$cache->set('key','some data');
null
I find DbCache php file, change that setValue() method,add a line below:
protected function setValue($key, $value, $duration)
{
$command = $this->db->createCommand()
->update($this->cacheTable, [
'expire' => $duration > 0 ? $duration + time() : 0,
'data' => [$value, \PDO::PARAM_LOB],
], ['id' => $key]);
if ($command->execute()) {
$this->gc();
return true;
} else {
/*======add this line===========*/
$this->deleteValue($key);//before addValue,delete the duplicate key if any.
/*========================*/**
return $this->addValue($key, $value, $duration);
}
}
that is the revolution。
Integrity constraint violation: 1062 Duplicate entry 'key' for key 'PRIMARY'
when I use FileCache,it won't throw exception,and It will refresh the data every call set method。
| Q | A
| ---------------- | ---
| Yii version | 2.0.11.2
| PHP version | 7.0
| Operating system | Windows
MySQL will return 0 on an UPDATE execute() if no data is changed.
This behaviour can be configured as per this comment but won't be applicable to other db's http://php.net/manual/en/pdostatement.rowcount.php#104930
@samdark I already fixed this problem in early 2018. See commit 13ad205, PR #16002. Upsert is now used instead of insert/update.
Related issue #14660.
It can be closed.
Most helpful comment
@samdark I already fixed this problem in early 2018. See commit 13ad205, PR #16002. Upsert is now used instead of insert/update.
Related issue #14660.
It can be closed.