PHP code example of icy8 / php-lock-system
1. Go to this page and download the library: Download icy8/php-lock-system 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/ */
icy8 / php-lock-system example snippets
\icy8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
// 默认使用的是redis锁
$lxs->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
sleep(2);
});
\icy8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->redis()->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
sleep(2);
});
\icy8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->redis([
'host'=>'127.0.0.1',
'port'=>'6379',
'password'=>'123456',
])->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
sleep(2);
});
\icy8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->file()->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
sleep(2);
});
\icy8\PHPLock\Client;
// 第二个参数支持闭包、数组(对象)、字符串(函数名)。
Client::newRun("test_lock_userid", function ($that, $v1) {
// 第一个参数 $that 是当前运行的实例
// 第二个参数起即为自定义传入的参数值
var_dump($v1);
var_dump("running pid: " . $that->getPid());
sleep(2);
}, "这里是自定义参数");
\icy8\PHPLock\Client;
class Business
{
public function handle($lxs)
{
var_dump("running pid: " . $lxs->getPid());
sleep(2);
}
}
$lxs = new Client("test_lock_userid");
$lxs->redis()->run([new Business, "handle"], $lxs);
icy8\PHPLock\Client;
function Business($lxs, $v1)
{
var_dump($v1);
var_dump("running pid: " . $lxs->getPid());
sleep(2);
}
$lxs = new Client("test_lock_userid");
$lxs->redis()->run("Business", $lxs, "aaa");
\icy8\PHPLock\Client;
function Business($lxs, $v1)
{
var_dump($v1);
var_dump("running pid: " . $lxs->getPid());
sleep(2);
}
$lxs = new Client("test_lock_userid");
// 绑定业务函数
$lxs->bindEvent("Business");
// 或者绑定对象方法
// $lxs->bindEvent([new Business, 'handle']);
// 开始运行。并传入所需的业务参数。
$lxs->redis()->run($lxs, "aaa");
8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->lockTimeoutException = true;// 超时后抛出异常
$lxs->redis()->run(function () use ($lxs) {
var_dump("running pid: " . $lxs->getPid());
sleep(10);
});
8\PHPLock\Client;
$pdo = new \PDO("mysql:dbname=testdb;host=127.0.0.1", "root", "root");
$pdo->beginTransaction();
$lxs = new Client("test_lock_userid");
$lxs->redis()->run(function () use ($lxs) {
// 这样写虽然不会报错,但是数据并发问题依然存在
var_dump("running pid: " . $lxs->getPid());
sleep(2);
});
$pdo->commit();
\icy8\PHPLock\Client;
$pdo = new \PDO("mysql:dbname=testdb;host=127.0.0.1", "root", "root");
$lxs = new Client("test_lock_userid");
$lxs->redis()->run(function () use ($lxs, $pdo) {
// 事务代码最好是放到锁里面运行
$pdo->beginTransaction();
var_dump("running pid: " . $lxs->getPid());
sleep(2);
$pdo->commit();
});
8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
// 这是一段业务流程代码
if(1==1) {
die();// 直接退出脚本
}
sleep(2);
});
8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
// 这是一段业务流程代码
if(1==1) {
throw new \Exception("终止业务逻辑闭包");// 通过抛出异常来终止闭包
}
sleep(2);
});
\icy8\PHPLock\Client;
$lxs = new Client("test_lock_userid");
$lxs->run(function () use ($lxs) {
// 业务逻辑放到闭包运行
var_dump("running pid: " . $lxs->getPid());
// 这是一段业务流程代码
if(1==1) {
// 手动释放掉当前的锁
$lxs->unlock();
die;
}
sleep(2);
});