yiisoft / yii2-redis

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

The save() method is non-atomic and primary keys can be repeat #220

Open haohetao opened 3 years ago

haohetao commented 3 years ago

What steps will reproduce the problem?

The save() method is non-atomic, various problems can occur in highly concurrent operations. and it's inefficient.

What's expected?

Use the Lua script to implement the save operation, and primary keys must be unique.

What do you get instead?

Refactoring the insert method in ActiveRecord class.

$luaScript = <<<EOF
if redis.call('EXISTS', $key) == 0 then
    redis.call('RPUSH', $prefixKey, $pkKey)
end
redis.call('HMSET', $setArgs)
return 1
EOF;

Additional info

Q A
Yii vesion 2.0.40
PHP version 8.0.1
Operating system docker
samdark commented 3 years ago

Sounds interesting. How about preparing a pull request with tests and implementation?