PHP code example of awheel / redis-lock

1. Go to this page and download the library: Download awheel/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/ */

    

awheel / redis-lock example snippets

`


namespace awheel\RedisLock;

 RedisLockAbstract
{
    /**
     * 获取 Redis 对象
     *
     * @return Redis
     */
    static public function getInstance()
    {
        self::$redis = new Redis();
        self::$redis->connect('127.0.0.1');

        return self::$redis;
    }
}

$key = 'redis_lock_test';
if (RedisLock::lock($key)) {
    printf("locked\n");

    // do something
    RedisLock::renew($key, 5);

    $unlock = RedisLock::unlock($key);
    $unlock && printf("unlocked\n");
}