yiisoft / yii2-redis

Yii 2 Redis extension.
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
452 stars 183 forks source link

Request an efficient updateCounters() operation #221

Open haohetao opened 3 years ago

haohetao commented 3 years ago

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
samdark commented 3 years ago

The idea is alright. The operation is usually called "upsert". Do you have time for a pull request with unit tests and implementation?

haohetao commented 3 years ago

In contrast to the upsert operation in the relational database, HINCRBY is used and can only be used to accumulate integer value.

samdark commented 3 years ago

Ah. Alright. Then the name is OK.