PHP code example of friendsofhyperf / lock

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

    

friendsofhyperf / lock example snippets


$lock = lock($name = 'foo', $seconds = 10, $owner = null);

if ($lock->get()) {
    // Lock acquired for 10 seconds...

    $lock->release();
}

lock('foo')->get(function () {
    // Lock acquired indefinitely and automatically released...
});

use FriendsOfHyperf\Lock\Exception\LockTimeoutException;

$lock = lock('foo', 10);

try {
    $lock->block(5);

    // Lock acquired after waiting maximum of 5 seconds...
} catch (LockTimeoutException $e) {
    // Unable to acquire lock...
} finally {
    optional($lock)->release();
}

lock('foo', 10)->block(5, function () {
    // Lock acquired after waiting maximum of 5 seconds...
});

use FriendsOfHyperf\Lock\Annotation\Lock;
use FriendsOfHyperf\Lock\Driver\LockInterface;

class Foo
{
    #[Lock(name:"foo", seconds:10)]
    protected LockInterface $lock;

    public function bar()
    {
        $this->lock->get(function () {
            // Lock acquired indefinitely and automatically released...
        });
    }
}
shell
php bin/hyperf.php vendor:publish friendsofhyperf/lock -i config