PHP code example of pizsd / hyperf-redis-lock

1. Go to this page and download the library: Download pizsd/hyperf-redis-lock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pizsd / hyperf-redis-lock example snippets


    #[Inject]
    protected Redis $redis;

    public function __construct(RedisFactory $redisFactory)
    {
        $this->redis = $redisFactory->get('default');
    }

public function lock(ResponseInterface $response)
{
    $lock = new RedisLock();
    $res = $lock->get(lock, 20 *100, function () {
        sleep(10);
        return [123];
    });
    return $response->json($res);
}

/**
 * @return \Psr\Http\Message\ResponseInterface
 */
public function lockA(ResponseInterface $response)
{
    try {
        $lock = new RedisLock();
        $res = $lock->block('lock', 44, function () {
            return [456];
        });
        return $response->json(['res' => $res]);
    // catch the exception
    } catch (LockTimeoutException $exception) {
        var_dump('lockA lock check timeout');
        return $response->json(['res' => false, 'message' => 'timeout']);
    }
}