/php-redis-lock

🐘PHP use Redis Pessimistic🔒Lock uses redis keys: setnx, get, getset, del

Primary LanguagePHPMIT LicenseMIT

RedisLock

PHP use Redis Pessimistic Lock uses redis keys: setnx, get, getset, del

Install

composer require ar414/redis-lock

Usage

New instance

use Ar414\RedisLock;

$redis = new \Redis();
$redis->connect('127.0.0.1','6379');

$lockTimeOut = 5;
$redisLock = new RedisLock($redis,$lockTimeOut);

Get Lock

$lockKey = 'lock:user:wallet:uid:1001';
$lockExpire = $redisLock->getLock($lockKey);
if(!$lockExpire || $lockExpire < time()){
    throw new \Exception('Busy Lock');
}
//TODO:Business logic

Release Lock

$redisLock->releaseLock($lockKey,$lockExpire);