PHP code example of aternos / lock

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

    

aternos / lock example snippets




$lock = new \Aternos\Lock\Lock("key");

// try to acquire lock
if($lock->lock()) {
    // do something
} else {
    // error/exit
}

 

$lock->lock(true);

 

$lock->lock(true, 60); // 60 seconds timeout

// refresh the lock (with default values)
$lock->refresh();

// refresh with 120 seconds timeout
$lock->refresh(120);

// refresh with 120 seconds timeout, but only if remaining time is lower than 60 seconds
$lock->refresh(120, 60);

 

$lock->lock(true, 60, 300); // wait 300 seconds for other locks

// check if the lock was actually acquired or if the wait timeout was reached
if($lock->isLocked()) {
    // do something
} else {
    // error/exit
}



$lock = new \Aternos\Lock\Lock("key");

// check if the lock was successful
if($lock->isLocked()) {
    // do something
    $lock->break();
} else {
    // error/exit
}



\Aternos\Lock\Lock::setDefaultIdentifier("default-identifier");

// uses the previously set "default-identifier"
$lock->lock();

// overwrites the "default-identifier" with "different-identifier"
$lock->lock(true, 60, 300, "different-identifier");



$client = new Aternos\Etcd\Client();
$client = new Aternos\Etcd\Client("localhost:2379");
$client = new Aternos\Etcd\Client("localhost:2379", "username", "password");

\Aternos\Lock\Lock::setClient($client);



\Aternos\Lock\Lock::setPrefix("my-prefix/");