Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pkg6/php-lock
基于php对常见的分布式锁方案封装
https://github.com/pkg6/php-lock
Last synced: 7 days ago
JSON representation
基于php对常见的分布式锁方案封装
- Host: GitHub
- URL: https://github.com/pkg6/php-lock
- Owner: pkg6
- License: mit
- Created: 2023-01-20T03:23:13.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-20T03:43:52.000Z (almost 2 years ago)
- Last Synced: 2024-08-11T09:32:51.177Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 锁实例化
~~~
//文件锁
$lock = new \Pkg6\Lock\FileLock('test');
$lock = \Pkg6\Lock\FileLock::getInstance('test');//redis锁
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$lock =new \Pkg6\Lock\RedisLock('test', $redis);
$lock = \Pkg6\Lock\RedisLock::getInstance('test', $redis);//memcache锁
$memcache = new \Memcache();
$memcache->connect('127.0.0.1', 11211, 120);
$lock =new \Pkg6\Lock\MemcacheLock('test', $memcache);
$lock = \Pkg6\Lock\MemcacheLock::getInstance('test', $memcache);//memcached锁
$memcached = new \Memcached();
$memcached->addServer('127.0.0.1', 11211);
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$lock = new \Pkg6\Lock\MemcachedLock('test', $memcached);
$lock = \Pkg6\Lock\MemcachedLock::getInstance('test', $memcached);
~~~## 锁基本使用
~~~
if (\Pkg6\Lock\Lock::LOCK_RESULT_SUCCESS === $lock->lock()) {
// 加锁后处理的任务
$lock->unlock();
}
~~~## 锁高级使用
~~~
$result = $lock->lock(
function(){
// 加锁后处理的任务
},
function(){
// 判断是否其它并发已经处理过任务
}
);
switch($result)
{
case \Pkg6\Lock\Lock::LOCK_RESULT_CONCURRENT_COMPLETE:
// 其它请求已处理
break;
case \Pkg6\Lock\Lock::LOCK_RESULT_CONCURRENT_UNTREATED:
// 在当前请求处理
break;
case \Pkg6\Lock\Lock::LOCK_RESULT_FAIL:
echo '获取锁失败', PHP_EOL;
break;
}
~~~