定制 texthtml/php-lock-redis 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

texthtml/php-lock-redis

最新稳定版本:v1.2.0

Composer 安装命令:

composer require texthtml/php-lock-redis

包简介

redis lock

README 文档

README

Build Status Latest Stable Version License Total Downloads Scrutinizer Code Quality

php-lock-redis is an extension for php-lock that makes locking on resources easy on distributed system using Redis. It can be used instead of file base locking to lock operations on a distributed system.

Installation

With Composer :

composer require texthtml/php-lock-redis

Usage

You can create an object that represent a lock on a resource. You can then try to acquire that lock by calling $lock->acquire(). If the lock fail it will throw an Exception (useful for CLI tools built with Symfony Console Components documentation). If the lock is acquired the program can continue.

Locking a ressource

use TH\RedisLock\RedisSimpleLockFactory;

$redisClient = new \Predis\Client;
$factory = new RedisSimpleLockFactory($redisClient);
$lock = $factory->create('lock identifier');

$lock->acquire();

// other processes that try to acquire a lock on 'lock identifier' will fail

// do some stuff

$lock->release();

// other processes can now acquire a lock on 'lock identifier'

Auto release

$lock->release() is called automatically when the lock is destroyed so you don't need to manually release it at the end of a script or if it goes out of scope.

use TH\RedisLock\RedisSimpleLockFactory;

function batch() {
    $redisClient = new \Predis\Client;
    $factory = new RedisSimpleLockFactory($redisClient);
    $lock = $factory->create('lock identifier');
    $lock->acquire();

    // lot of stuff
}

batch();

// the lock will be released here even if $lock->release() is not called in batch()

Limitations

Validity time

If a client crashes before releasing the lock (or forget to release it), no other clients would be able to acquire the lock again. To avoid Deadlock, RedisSimpleLock locks have a validity time at witch the lock key will expire. But be careful, if the operation is too long, another client might acquire the lock too.

Mutual exclusion

Because RedisSimpleLock does not implements the RedLock algorithm, it have a limitation : with a master slave replication, a race condition can occurs when the master crashes before the lock key is transmitted to the slave. In this case a second client could acquire the same lock.

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2015-10-18