PHP code example of remi-san / lock

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

    

remi-san / lock example snippets


use RemiSan\Lock\TokenGenerator\RandomTokenGenerator;

$tokenGenerator = new RandomTokenGenerator();
echo $tokenGenerator->generateToken(); // 'QcWY1WFoRTC68vTNIkTs5cuLmw9YuY9rwS6IsY0xjzA='

use RemiSan\Lock\TokenGenerator\FixedTokenGenerator;

$tokenGenerator = new FixedTokenGenerator('my_token');
echo $tokenGenerator->generateToken(); // 'my_token'

use RemiSan\Lock\LockStore\RedisLockStore;
use RemiSan\Lock\Locker\SingleStoreLocker;
use RemiSan\Lock\TokenGenerator\RandomTokenGenerator;
use Symfony\Component\Stopwatch\Stopwatch;

$connection = new \Redis();
$server->connect('127.0.0.1', 6379, 0.1);

$tokenGenerator = new RandomTokenGenerator();
$stopwatch = new Stopwatch();

$redLock = new SingleStoreLocker(
    new RedisLockStore($connection),
    $tokenGenerator,
    $stopwatch
);

use RemiSan\Lock\LockStore\RedisLockStore;
use RemiSan\Lock\Locker\MultipleStoreLocker;
use RemiSan\Lock\Quorum\MajorityQuorum;
use RemiSan\Lock\TokenGenerator\RandomTokenGenerator;
use Symfony\Component\Stopwatch\Stopwatch;

$connection1 = new \Redis();
$server->connect('127.0.0.1', 6379, 0.1);

$connection2 = new \Redis();
$server->connect('127.0.0.1', 6380, 0.1);

$tokenGenerator = new RandomTokenGenerator();
$quorum = new MajorityQuorum();
$stopwatch = new Stopwatch();

$redLock = new MultipleStoreLocker(
    [ new RedisLockStore($connection1), new RedisLockStore($connection2) ],
    $tokenGenerator,
    $quorum,
    $stopwatch
);

$lock = $locker->lock('my_resource_name', 1000, 3, 100);

$isLocked = $locker->isLocked('my_resource_name');

$locker->unlock($lock);