PHP code example of sdyyf / rlock

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

    

sdyyf / rlock example snippets


/* 简单锁 */
$lock = Rlock::getSimpleLock('lockname', 10);
if ($lock->get()) {
    echo 'get lock succeed.';
} else {
    echo 'get lock failed.'
}

/* 自旋锁 */
$lock = Rlock::getSpinLock('lockname', 10, [
    'timeout' => 10,
    'sleep'   => 100
]);
if ($lock->get()) {
    echo 'get lock succeed.';
} else {
    echo 'get lock failed.'
}

/* 排队锁 */
//使用默认设置
//$lock = Rlock::getQueueLock('lockname', 10); 
//使用自定义设置
$lock = Rlock::getQueueLock('lockname', 10, [
    'timeout'    => 10,
    'sleep'      => 100,//不设置该参数时,默认值为lock_config.queue_sleep配置项设定值,注意这里的参数名为sleep
    'max_length' => 10
]);
if ($lock->get()) {
    echo 'get lock succeed.';
} else {
    echo 'get lock failed.'
}

$lock = Rlock::getSimpleLock('lockname', 10);
//result为闭包执行结果,或者获取锁失败返回false
$result = $lock->get(function() {
    //your business logic...
});