Skip to content

ar414-com/php-redis-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages