yusitnikov / php-elock-client
PHP上的简单eLock客户端
This package is not auto-updated.
Last update: 2024-09-24 15:42:39 UTC
README
PHP上的简单eLock客户端。也支持NodeJS eLock服务器的功能。
什么是eLock?
eLock是一个基于erlang的简单分布式锁服务器。
优势
- 容错性
- 易于安装和使用
- 安全避免竞态条件 - 所有操作都是原子的
- 当客户端断开连接时,客户端所需的全部锁都将自动解锁
参见源代码仓库。
什么是NodeJS eLock服务器?
NodeJS eLock服务器是在NodeJS上实现原始eLock协议,并增加了死锁检测功能。
优势
- 原始eLock协议的所有优势
- 当锁尝试导致递归锁环时,将自动报告死锁,并以423响应代码返回
- 支持值锁
- 有"debug"命令
参见源代码仓库。
安装eLock服务器
原始eLock服务器
NodeJS eLock服务器
它将在11400端口上监听。
安装eLock客户端
客户端可以作为composer依赖项使用
composer install yusitnikov/php-elock-client
使用eLock客户端
基本用法
$key1 = 'unique-resource-key1'; $key1 = 'unique-resource-key2'; $lockTimeout = 5; // Create a client of an original eLock server $client = new ELockClient('your.elock.server.host.or.ip'); // or create a client of a NodeJS eLock server $client = new ELockClientEx('your.elock.server.host.or.ip'); // Tell to release all locks after the disconnection $client->setTimeout(0); // Lock keys $lockedKey1 = $client->lock($key1, $lockTimeout); $lockedKey2 = $client->lock($key2, $lockTimeout); // Unlock key $unlockedKey1 = $client->unlock($key1); // Unlock all keys you own $client->unlockAll(); // Disconnect from the server $client->quit(); $client->close();
原子操作的常用用法
// Lock the resource with a timeout that's big enough to wait for other clients to finish their job if (!$client->lock($key, $timeout)) { throw new Exception('Failed to lock the resource XXX during YYY seconds'); } try { // Do something with the locked resource } finally { // Ensure that the resource will be unlocked in the case of unexpected error $client->unlock($key); }