PHP code example of purelightme / redis-lock
1. Go to this page and download the library: Download purelightme/redis-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/ */
purelightme / redis-lock example snippets
$config = [
'host' => 'redis',
'name' => 'default',
'ttl' => 60,//原子锁ttl
'interval' => 5,//子进程自动续期周期
//...其他predis支持的参数
];
try{
$res = SequenceTask::execute($config,function (){
//fake long time logic...
sleep(20);
return 'job execute success';
});
}catch (InternalException $exception){
//一般情况下无需关注
$res = $exception->getMessage();
}catch (Throwable $exception){
//任务执行中本身的异常,需要关注
$res = $exception->getMessage();
}
var_dump($res);
php test/run.php