ar414/redis-lock

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

v1.0.0 2020-03-26 07:24 UTC

This package is auto-updated.

Last update: 2024-10-26 18:22:51 UTC


README

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