Giter Site home page Giter Site logo

laravel-redis-lock's Introduction

laravel-redis-lock

Total Downloads Latest Stable Version Latest Unstable Version License

Simple redis distributed locks for Laravel.

Getting started

Install

Using composer.

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

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"

Default items:

<?php

return [
    // Use app('redis')->connection('default')
    'connection' => 'default',
    'retry_count' => 3,
    'retry_delay' => 200,
];

Use

<?php

use RedisLock\Facades\RedisLock;

// Set the specified expire time, in milliseconds.
$millisecond = 100000;

// Try get lock.
// If has non-null property `$this->retryCount`, will retry some times with its value.
// Default value is `config('redislock.retry_count')`
$payload = RedisLock::lock('key', $millisecond);
/*
[
    "key" => "key",
    "token" => "21456004925bd1532e64616",
    "expire" => 100000,
    "expire_type" => "PX",
]
*/

// If cannot get lock, will return empty array.
$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 returned value is same to use RedisLock::lock()
RedisLock::relock($payload, $millisecond);

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

// Retry 5 times when missing the first time.
// Non-null `$retry` param will priority over `$this->retryCount`.
RedisLock::lock('key', 100000, 5);

// No retry (Try once only).
RedisLock::lock('key', 100000, 0);
// If value less than 0, still means try once only.
// RedisLock::lock('key', 100000, -1);
// Hmmmmmmm...Not pretty.

// Change property `$this->retryDelay` (Default value is `config('redislock.retry_delay')`).
// Retry 10 times when missing the first time.
// Every retry be apart 500 ~ 1000 milliseconds.
RedisLock::setRetryDelay(1000)->lock('key', 100000, 10);
// PS:
// RedisLock is default registered to singleton, call method `setRetryDelay()` will affects subsequent code.

// Use in business logic:
try {
    if (! $lock = RedisLock::lock('do-some-thing', 100000)) {
        throw new Exception('Resource locked.');
    }
    //////////////////////
    // Call ur methods. //
    //////////////////////
} catch (Exception $e) {
    throw $e;
} finally {
    RedisLock::unlock($lock);
}

laravel-redis-lock's People

Contributors

cidosx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-redis-lock's Issues

Laravel 6 & 7 support?

Could it be used in Laravel 6 and 7? If it's supported, could you update the dependencies of composer.json? Thanks.

Laravel 9

Hi,

Laravel 9 is out. Can you please update your composer.json as it's stopping us from upgrading to 9 as it's dependent on 8 (or lower).

Thanks!

support Laravel8๏ผŸ

I am using Laravel 8.14.0

when install laravel-redis-lock

Problem 1
- ginnerpeace/laravel-redis-lock v2.3.0 requires illuminate/redis ^5.1 -> found illuminate/redis[v5.1.1, ..., 5.8.x-dev] but these were not loaded, likely because it conflicts with another require.
- ginnerpeace/laravel-redis-lock v2.3.1 requires illuminate/redis ^5.1|^6.0|^7.0 -> found illuminate/redis[v5.1.1, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev] but these were not loaded, likely because it conflicts with another require.
- Root composer.json requires ginnerpeace/laravel-redis-lock ~2.3 -> satisfiable by ginnerpeace/laravel-redis-lock[v2.3.0, v2.3.1].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.