PHP code example of ginnerpeace / redlock-php

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

    

ginnerpeace / redlock-php example snippets



use Ginnerpeace\RedLock\RedLock;

// You can use any redis component to build the instance,
// Redis instance just need to implement the method: eval
$redLock = new RedLock([
    'servers' => [
        [
            // For ext-redis
            function ($host, $port, $timeout) {
                $redis = new \Redis();
                $redis->connect($host, $port, $timeout);
                return $redis;
            },
            ['127.0.0.1', 6379, 0.01]
        ],
        [
            // For Predis
            function ($dsn) {
                return new Predis\Client($dsn);
            },
            ['tcp://10.0.0.1:6379']
        ],
        [
            // For Laravel
            function ($name) {
                return RedisFacade::connection($name)->client();
            },
            ['redis']
        ],
    ],
]);


$lock = $redLock->lock('my_resource_name', 1000);

// Or use dynamic retry param.
$retryTime = 10;
$lock = $redLock->lock('my_resource_name', 1000, $retryTime);

[
    'validity' => 9897.3020019531
    'resource' => 'my_resource_name',
    'token' => '22f8fd8d0f176ee2e1b7e676ae1f6c8b',
];

$redLock->unlock($lock);
bash
composer