2017 © Pedro Peláez
 

library redis-lock

use redis to control concurrent.

image

quhang/redis-lock

use redis to control concurrent.

  • Friday, March 23, 2018
  • by quhang
  • Repository
  • 1 Watchers
  • 1 Stars
  • 43 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 8 % Grown

The README.md

Redis Lock

Use redis feature to implement lock. I use this to control concurrent., (*1)

Laravel

laravel 5.5+

you can publish config, then set redis connection in .env REDIS_LOCK_CONNECTION., (*2)

php artisan vendor:publish

laravel 5.1 ~ 5.4

if you want to config your webhook, you need add Quhang\RedisLock\Laravel\ServiceProvider to config/app.php, (*3)

'providers' => [
    // ...
    Quhang\RedisLock\Laravel\ServiceProvider::class,
    // ...
]

add Quhang\RedisLock\Laravel\Facades\Lock to config/app.php, (*4)

'aliases' => [
    // ...
    'Lock' => Quhang\RedisLock\Laravel\Facades\Lock::class,
    // ...
]

usage

get method will return true or false.

    try {
        $rest = \Quhang\RedisLock\Laravel\Facades\Lock::get($key);
        if ($rest) {
            // get lock success
        } else {
            // get lock fail
        }
    } finally {
        \Quhang\RedisLock\Laravel\Facades\Lock::release($key);
    }

tryToGet method will be block until get lock or timeout.

    try {
        \Quhang\RedisLock\Laravel\Facades\Lock::tryToGet($key, $timeout = 10);
    } catch (Quhang\RedisLock\TimeoutException $e) {
        // timeout exception
    } finally {
        \Quhang\RedisLock\Laravel\Facades\Lock::release($key);
    }

Other

$client = new \Predis\Client($config, $option);
$lock = new \Quhang\RedisLock\Lock($client);

// try to get lock for key once
try {
    $lock->get($key);
    // your logic
} finally {
    $lock->release($key);
}

######################################################

// try to get lock for key many times untill timeout
try {
    $lock->tryToGet($key, $timeout = 10);
    // your logic
} catch(\Quhang\RedisLock\TimeoutException $e) {
    // timeout handle
} finally {
    $lock->release($key);
}

license

MIT, (*5)

The Versions

23/03 2018

dev-master

9999999-dev

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

23/03 2018

0.0.8

0.0.8.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

21/03 2018

0.0.7

0.0.7.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

21/03 2018

0.0.6

0.0.6.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

20/03 2018

0.0.5

0.0.5.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

20/03 2018

0.0.4

0.0.4.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

20/03 2018

0.0.3

0.0.3.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

15/03 2018

0.0.2

0.0.2.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock

07/03 2018

0.0.1

0.0.1.0

use redis to control concurrent.

  Sources   Download

MIT

The Requires

 

by Avatar quhang

redis lock