PHP code example of warmans / dlock

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

    

warmans / dlock example snippets


//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//datastore adapter
$adapter = new \Dlock\Datastore\Memcache($memcache);

$lock = new \Dlock\Lock($adapter);
$lock->locked(function(){
    //do something
});

//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//datastore adapter
$adapter = new \Dlock\Datastore\Memcache($memcache);

$lock = new \Dlock\Lock($adapter);
if ($lock->acquire()) {
    //do something
    $lock->release();
} else {
   //handle lock failure
}

//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//datastore adapter
$adapter = new \Dlock\Datastore\Memcache($memcache);

$lock = new \Dlock\Lock($adapter);

//wait for up to 30 seconds for lock then return false
if ($lock->acquire(30)) {
    //do something
    $lock->release();
} else {
   //handle lock failure
}

//or use closure with 30 second block
$lock->locked(function(){
    //do something
}, 30);

$lock = new Lock([...], [...], $options);

interface DatastoreInterface
{
    public function acquireLock($lockId);
    public function releaseLock($lockId);
}

//raw memcache connection
$memcache = new \Memcache();
$memcache->connect('localhost');

//configure adapter with localhost connction and an hour TTL on locks
$adapter = new \Dlock\Datastore\Memcache($memcache, 3600);

//raw redis connection
$redis = new \Redis();
$redis->connect('localhost');

//configure adapter with localhost connction and an hour TTL on locks
$adapter = new \Dlock\Datastore\Redis($memcache, 3600);