liupei / lock
lock anything
This package's canonical repository appears to be gone and the package has been frozen as a result.
1.0.2
2019-08-20 13:56 UTC
Requires (Dev)
- php: >=5.6
This package is not auto-updated.
Last update: 2020-08-05 17:12:55 UTC
README
该扩展基于redis实现,帮助你实现任何你想锁住的东西,我没有吹牛,请继续往下看
安装
执行以下命令来安装该扩展
composer require liupei/lock
使用指南
$config = [
'type' => 'redis', // 类型,目前只支持redis
'host' => '127.0.0.1', // redis host
'port' => '6379', // redis port
'auth' => 'xxxxxxx', // redis uath
'select_db' => 6, // redis db
'lock_wait_timeout' => 10, // 锁超时等待时间,单位秒
];
$myLock = new liupei\lock\Lock($config);
// 锁住指定文件,5秒
$key = './test.txt';
$result = $myLock->lock($key, 5);
if ($result === false) {
echo $myLock->getError();
die();
}
$content = '时间:' . date('Y-m-d H:i:s') . PHP_EOL;
file_put_contents($key, $content, FILE_APPEND);
// 手动解锁,如果不手动解锁5秒后会自动解锁
$myLock->unlock($key);