定制 gaillard/mongo-lock 二次开发

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

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

gaillard/mongo-lock

Composer 安装命令:

composer require gaillard/mongo-lock

包简介

Distributed multi-reader lock using MongoDB

README 文档

README

Build Status

Distributed multi-reader lock using MongoDB

Requirements

Requires PHP 5.4.0 (or later).

Installation

To add the library as a local, per-project dependency use Composer!

{
    "require": {
        "gaillard/mongo-lock": "~1.0"
    }
}

Example

$writer = function($value) {
    $db = (new \MongoClient())->selectDB('locksExample');
    $data = $db->selectCollection('data');
    $locker = new Locker($db->selectCollection('locks'), 0);

    while (true) {
        $locker->writeLock('theId', 1000);

        $data->update(['_id' => 1], ['_id' => 1, 'key' => $value], ['upsert' => true]);
        $data->update(['_id' => 2], ['_id' => 2, 'key' => $value], ['upsert' => true]);
        $data->update(['_id' => 3], ['_id' => 3, 'key' => $value], ['upsert' => true]);
        $data->update(['_id' => 4], ['_id' => 4, 'key' => $value], ['upsert' => true]);

        $locker->writeUnlock('theId');
    }
};

$reader = function() {
    $db = (new \MongoClient())->selectDB('locksExample');
    $data = $db->selectCollection('data');
    $locker = new Locker($db->selectCollection('locks'), 100000);

    while (true) {
        $readerId = $locker->readLock('theId', 1000);

        foreach ($data->find()->sort(['_id' => 1]) as $doc) {
            echo "{$doc['key']} ";
        }

        echo "\n";

        $locker->readUnlock('theId', $readerId);

        usleep(100000);
    }
};

$writerOnePid = pcntl_fork();
if ($writerOnePid === 0) {
    $writer('pie');
    exit;
}

$writerTwoPid = pcntl_fork();
if ($writerTwoPid === 0) {
    $writer('cake');
    exit;
}

$readerOnePid = pcntl_fork();
if ($readerOnePid === 0) {
    $reader();
    exit;
}

$readerTwoPid = pcntl_fork();
if ($readerTwoPid === 0) {
    $reader();
    exit;
}

sleep(4);

posix_kill($writerOnePid, SIGTERM);
posix_kill($writerTwoPid, SIGTERM);
posix_kill($readerOnePid, SIGTERM);
posix_kill($readerTwoPid, SIGTERM);

prints something similiar too

pie pie pie pie
pie pie pie pie pie
pie pie pie
pie pie pie pie pie pie pie pie

cake cake cake cake cake cake cake
cake
pie pie pie pie
cake cake cake cake
pie pie pie pie
cake cake cake cake

You'll notice that all the pies and cakes are always seperated by a newline. That is because there are no readers with a lock during writing. That also indicates both writers not writing at the same time. Sometimes when running the example there are more or less than four printed before a newline. That is the case when the two readers have a lock at the same time.

Contributing

If you would like to contribute, please use the build process for any changes and after the build passes, send a pull request on github!

./build.php

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固