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

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

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

signe/redlock-php

最新稳定版本:1.1.0

Composer 安装命令:

composer require signe/redlock-php

包简介

Redis distributed locks in PHP

README 文档

README

redlock-php - Redis distributed locks in PHP

Based on Redlock-rb by Salvatore Sanfilippo, descendant of ronnylt/redlock-php which was abandoned in early 2015.

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

To create a lock manager:

Locks can be provided using an array of server tuples made up of the server name (required), connection port (optional), and connection timeout (optional), or by using already-connected Redis objects,

$server = new \Redis;
$server->connect('127.0.0.1');

$servers = [
    ['127.0.0.1', 6379, 0.01],
    ['127.0.0.1'],
    $server,
];

$redLock = new RedLock($servers);

To acquire a lock:

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

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 false if the lock was not acquired (you may try again), otherwise an array representing the lock is returned, having three keys:

Array
(
    [validity] => 9897.3020019531
    [resource] => my_resource_name
    [token] => 53771bfa1e775
)
  • 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.

统计信息

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

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-10-14