PHP code example of yusitnikov / php-elock-client

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

    

yusitnikov / php-elock-client example snippets


$key1 = 'unique-resource-key1';
$key1 = 'unique-resource-key2';
$lockTimeout = 5;

// Create a client of an original eLock server
$client = new ELockClient('your.elock.server.host.or.ip');
// or create a client of a NodeJS eLock server
$client = new ELockClientEx('your.elock.server.host.or.ip');

// Tell to release all locks after the disconnection
$client->setTimeout(0);

// Lock keys
$lockedKey1 = $client->lock($key1, $lockTimeout);
$lockedKey2 = $client->lock($key2, $lockTimeout);

// Unlock key
$unlockedKey1 = $client->unlock($key1);

// Unlock all keys you own
$client->unlockAll();

// Disconnect from the server
$client->quit();
$client->close();

// Lock the resource with a timeout that's big enough to wait for other clients to finish their job
if (!$client->lock($key, $timeout)) {
    throw new Exception('Failed to lock the resource XXX during YYY seconds');
}

try {
    // Do something with the locked resource
} finally {
    // Ensure that the resource will be unlocked in the case of unexpected error
    $client->unlock($key);
}
cmd
composer install yusitnikov/php-elock-client