Implement lock mechanism using PHPRedis
P粉515066518
P粉515066518 2023-08-28 18:33:09
0
2
485

I got the following two functions for locking the Redis key. I'm trying to prevent concurrent execution of a block of code using Redis. So what I did was use the following function to prevent different threads from executing the same code.

lockRedisKey("ABC"); CODE THAT I DON'T WANT TO RUN CONCURRENTLY! unlockRedisKey("ABC");

Unfortunately, it doesn't seem to work and causes lockRedisKey() to loop infinitely until exit_time is reached. What could possibly go wrong?

static public function lockRedisKey($key, $value = "true") { $redis = RedisClient::getInstance(); $time = microtime(true); $exit_time = $time 10; $sleep = 10000; do { // Lock Redis with PX and NX $lock = $redis->setnx("lock:" . $key, $value); if ($lock == 1) { $redis->expire("lock:" . $key, "10"); return true; } usleep($sleep); } while (microtime(true) < $exit_time); return false; } static public function unlockRedisKey($key) { $redis = RedisClient::getInstance(); $redis->del("lock:" . $key); }

I knew I might face a deadlock, so I decided to use transactions, but I'm still facing this problem.

static public function lockRedisKey($key, $value = "true") { $redis = RedisClient::getInstance(); $time = microtime(true); $exit_time = $time 10; $sleep = 10000; do { // Lock Redis with PX and NX $redis->multi(); $redis->set('lock:' . $key, $value, array('nx', 'ex' => 10)); $ret = $redis->exec(); if ($ret[0] == true) { return true; } usleep($sleep); } while (microtime(true) < $exit_time); return false; } static public function unlockRedisKey($key) { $redis = RedisClient::getInstance(); $redis->multi(); $redis->del("lock:" . $key); $redis->exec(); }

P粉515066518
P粉515066518

reply all (2)
P粉386318086

Your "exit_time" is too small. I think $exit_time = $time 10; means close it after 10 seconds. However, the set time is micro time.

Maybe you can replace '$exit_time = $time 10;' with '$exit_time = $time 100000;'. Written by Google Translate

    P粉336536706

    Locking works fine. This just crashes the code between locks and causes the lock to not be released :-)

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!