yii2-redis
yii2-redis copied to clipboard
Request an efficient updateCounters() operation
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?
/**
* 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;
}
Additional info
| Q | A |
|---|---|
| Yii vesion | 2.0.40 |
| PHP version | 8.0.1 |
| Operating system | docker |
The idea is alright. The operation is usually called "upsert". Do you have time for a pull request with unit tests and implementation?
In contrast to the upsert operation in the relational database, HINCRBY is used and can only be used to accumulate integer value.
Ah. Alright. Then the name is OK.