cevin / lock
php open lock
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/cevin/lock
This package is auto-updated.
Last update: 2025-10-12 03:26:16 UTC
README
usage
require __DIR__.'/vendor/autoload.php'; $driver = new Cevin\Lock\Driver\Redis('localhost'); $lock = new Cevin\Lock\Lock($driver); $lockKey = sprintf('goods:%d',1); $userId = 1; $lockExpire = 10; $waitSecondsForGetingLock = 10; $status = $lock->waitLock($lockKey,$userId,$lockExpire,$waitSecondsForGetingLock); sleep(5);// another processor status will be waiting # sleep(11); // current lock will be expired # sleep(11); // lock fail, $status=false $lock->unlock($lockKey); if ($lock->waitLock($lockKey,$uid,10,5)) { // do something $lock->unlock($lockKey); }
custom driver
class dbDriver implements Driver { public function tryGetLock($name, $content, $expire) { if(DB::select('exists lock')) return false; else return DB::query('insert...'); } public function unlock($name) { return DB::from('lock')->where('pk',$name)->delete(); } public function viewLock($name) { return DB::select('content')->from('lock')->where('pk',$name)->value(); } } $lock = new Lock(new dbDriver());