PHP code example of phlib / mutex

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

    

phlib / mutex example snippets


$adapter = new \Phlib\Db\Adapter([
    'host' => '127.0.0.1',
    'username' => 'my-user',
    'password' => 'my-pass'
]); 
$mutex = new \Phlib\Mutex\MySQL('my-lock', $adapter);
if ($mutex->lock()) {
    // Do some data manipulation while locked
    $mutex->unlock();
}

$getClosure = function() {
    // attempt to get a value, eg. from DB, cache, etc.
    if (!$value) {
        throw new \Phlib\Mutex\NotFoundException();
    }
    return $value;
};

$createClosure = function() {
    // attempt to create a value and write eg. to DB, cache, etc.
    return $value;
};

$value = \Phlib\Mutex\Helper::getOrCreate($mutex, $getClosure, $createClosure);