jay-youngn / laravel-redis-lock by Cidos

Simple redis distributed locks for Laravel.
81,083
13
2
Package Data
Maintainer Username: Cidos
Maintainer Contact: ginnerpeace@gmail.com (JayYoungn)
Package Create Date: 2018-10-14
Package Last Update: 2024-11-18
Home Page: https://packagist.org/packages/ginnerpeace/laravel-redis-lock
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:20:50
Package Statistics
Total Downloads: 81,083
Monthly Downloads: 2,293
Daily Downloads: 73
Total Stars: 13
Total Watchers: 2
Total Forks: 6
Total Open Issues: 0

laravel-redis-lock

Total Downloads Latest Stable Version Latest Unstable Version License

Simple mutex lock, redis edition.

Getting started

Install

Using composer.

composer require "ginnerpeace/laravel-redis-lock:~2.2"

Add service provider:

Normally.

<?php
return [
    // ....
    'providers' => [
        // ...
        RedisLock\Providers\RedisLockServiceProvider::class,
    ],
    // Its optional.
    'aliases' => [
        // ...
        'RedisLock' => RedisLock\Facades\RedisLock::class,
    ],
    // ...
];

After Laravel 5.5, the package auto-discovery is supported.

{
    "providers": [
        "RedisLock\\Providers\\RedisLockServiceProvider"
    ],
    "aliases": {
        "RedisLock": "RedisLock\\Facades\\RedisLock"
    }
}

Lumen

$app->register(RedisLock\Providers\LumenRedisLockServiceProvider::class);

Publish resources (laravel only)

Copied config to config/redislock.php.

php artisan vendor:publish --provider="RedisLock\Providers\RedisLockServiceProvider"

Use

<?php

use RedisLock\Facades\RedisLock;

// Ex. 1
$millisecond = 100000;

$payload = RedisLock::lock('key', $millisecond);
/*
[
    "key" => "key",
    "token" => "21456004925bd1532e64616",
    "expire" => 100000,
    "expire_type" => "PX",
]
*/

// Ex. 2
$second = 100;

/**
 * Its singleton instance, same as the following:
 * RedisLock::setExpireType('EX');
 * RedisLock::lock('key', $second);
 */
$payload = RedisLock::setExpireType('EX')->lock('key', $second);
/*
[
    "key" => "key",
    "token" => "5069829505bd154c8bb865",
    "expire" => 100,
    "expire_type" => "EX",
]
*/

// Is locking.
$payload = RedisLock::lock('key', 100000);
/*
[]
*/

// Return bool.
RedisLock::unlock($payload);

// Determine a lock if it still effective.
RedisLock::verify($payload);

// Reset a lock if it still effective.
// The 'expire' param is same to use RedisLock::lock()
RedisLock::relock($payload, 100000);

/////////////////////
// Special usages: //
/////////////////////

// Try 5 times if can't get lock at first hit.
// Default value is property: retryCount it from config('redislock.retry_count')
RedisLock::lock('key', 100000, 5);

// Try once only.
RedisLock::lock('key', 100000, 1);
// If value less than 1, will try at least once.
// But not pretty...
// RedisLock::lock('key', 100000, 0);
// RedisLock::lock('key', 100000, -1);

// Change default retry delay.
// Try 10 times & every retry be apart 500 ~ 1000 milliseconds.
RedisLock::setRetryDelay(1000)->lock('key', 100000, 10);