denismitr/mutex 问题修复 & 功能扩展

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

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

denismitr/mutex

最新稳定版本:v0.1

Composer 安装命令:

composer require denismitr/mutex

包简介

PHP Mutex Library

README 文档

README

Author

Denis Mitrofanov

Installation

composer require denismitr/mutex

Usage

Initialization with factory:

  • File lock
$lock = MutexFactory::fileLock(__FILE__); // or some other file name like /tmp/some-id
  • Semaphore lock (linux only)
$lock = MutexFactory::semaphoreLock(__FILE__); // or some other file name like /tmp/some-id
  • PRedis lock
$this->redis = new Client([
    'host' => 'localhost',
    'port' => 6379,
    'database' => 0,
]);

$this->lock = MutexFactory::pRedisLock($this->redis, "some-key", 20);

This far only these types of locks are supported

Using the lock instances

$lock->acquire();

// Do some critical stuff here

$lock->release();

With closures

$lock->safe(function() {
    // Lock will be acuqired and released automatically
    
    // Do some critical stuff safely
});

Performing a check first

$lock->try(function() use ($room, $from, $to) {
    // e.g
    return $room->isFree($from, $to);
})->then(function() use ($room, $from, $to) {
    // e.g.
    // Lock is aquired automatically
    
    $room->book($from, $to);
})->fail(function() use ($user) {
    // this callback will fire if the condition in try closure fails
    // e.g.
    $user->notify("Room is not available for requested time period.");
});

Looping in the safe, locked mode

$lock->loop($timeoutInSeconds, function($loop, $i) ($user, $ads) {
    // lock is acquired and released automatically when loop is done
    // e.g. send out only 10 ads to user friends
    
    // Laravel collections example
    $user->friends->each->notify($adds->random());
    
    if ($i >= 10) {
        $loop->stop();
    }
});

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-21