Every updateCounters() operation requires data to be query (determine whether the data exists) and locks to be used, which is very inefficient
Combine save and updateCounters in a single atomic operation to implement a lockless counter operation
/**
* insert a record, if exists then update counter
* @param bool $runValidation
* @param null $attributes
* @return bool
* @throws \yii\db\Exception
*/
public function insertOrUpdateCounter($runValidation = true, $attributes = null)
{
if ($runValidation && !$this->validate($attributes)) {
return false;
}
if (!$this->beforeSave(true)) {
return false;
}
$db = static::getDb();
$values = $this->getAttributes($attributes);
$pk = [];
foreach (static::primaryKey() as $key) {
$pk[$key] = $values[$key] = $this->getAttribute($key);
}
// save pk in a findall pool
$pk = static::buildKey($pk);
$pkKey = $this->quoteValue(static::buildKey($pk));
$prefix = static::keyPrefix();
$prefixKey = $this->quoteValue($prefix);
$key = $this->quoteValue($prefix . ':a:' . $pk);
$counterAttribute = static::counterKey();
$counterValue = $this->$counterAttribute;
$counterKey = $this->quoteValue($counterAttribute);
// save attributes
$setArgs = $key;
foreach ($values as $attribute => $value) {
// only insert attributes that are not null
if ($value !== null) {
if (is_bool($value)) {
$value = (int) $value;
}
$setArgs.= ', ' . $this->quoteValue($attribute);
$setArgs.= ',' . $this->quoteValue($value);
}
}
$luaScript = <<<EOF
if redis.call('EXISTS', $key) == 1 then
redis.call('HINCRBY' , $key, $counterKey, $counterValue)
return 0
else
redis.call('RPUSH', $prefixKey, $pkKey)
redis.call('HMSET', $setArgs)
end
return 1
EOF;
$db->executeCommand('eval', [$luaScript, 0]);
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $changedAttributes);
return true;
}
What steps will reproduce the problem?
Every updateCounters() operation requires data to be query (determine whether the data exists) and locks to be used, which is very inefficient
What's expected?
Combine save and updateCounters in a single atomic operation to implement a lockless counter operation
What do you get instead?
Additional info