ginnerpeace/redlock-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ginnerpeace/redlock-php

Composer 安装命令:

composer require ginnerpeace/redlock-php

包简介

Redis distributed locks in PHP

README 文档

README

Total Downloads Latest Stable Version Latest Unstable Version License

Based on Redlock-php by Ronny López

Based on Redlock-rb by Salvatore Sanfilippo

This library implements the Redis-based distributed lock manager algorithm described in this blog post.

Install

composer require ginnerpeace/redlock-php

Example

To create a lock manager

use Ginnerpeace\RedLock\RedLock;

// You can use any redis component to build the instance,
// Redis instance just need to implement the method: eval
$redLock = new RedLock([
    'servers' => [
        [
            // For ext-redis
            function ($host, $port, $timeout) {
                $redis = new \Redis();
                $redis->connect($host, $port, $timeout);
                return $redis;
            },
            ['127.0.0.1', 6379, 0.01]
        ],
        [
            // For Predis
            function ($dsn) {
                return new Predis\Client($dsn);
            },
            ['tcp://10.0.0.1:6379']
        ],
        [
            // For Laravel
            function ($name) {
                return RedisFacade::connection($name)->client();
            },
            ['redis']
        ],
    ],
]);

To acquire a lock

$lock = $redLock->lock('my_resource_name', 1000);

// Or use dynamic retry param.
$retryTime = 10;
$lock = $redLock->lock('my_resource_name', 1000, $retryTime);

Where the resource name is an unique identifier of what you are trying to lock and 1000 is the number of milliseconds for the validity time.

The returned value is [] if the lock was not acquired (you may try again), otherwise an array representing the lock is returned, having three keys:

[
    'validity' => 9897.3020019531
    'resource' => 'my_resource_name',
    'token' => '22f8fd8d0f176ee2e1b7e676ae1f6c8b',
];
  • validity, an integer representing the number of milliseconds the lock will be valid.
  • resource, the name of the locked resource as specified by the user.
  • token, a random token value which is used to safe reclaim the lock.

To release a lock

$redLock->unlock($lock);

It is possible to setup the number of retries (by default 3) and the retry delay (by default 200 milliseconds) used to acquire the lock.

The retry delay is actually chosen at random between $retryDelay / 2 milliseconds and the specified $retryDelay value.

Disclaimer: As stated in the original antirez's version, this code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in your production environments.

统计信息

  • 总下载量: 18.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固