承接 pudongping/wise-locksmith 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

pudongping/wise-locksmith

Composer 安装命令:

composer require pudongping/wise-locksmith

包简介

Mutex library for exclusive code execution.

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version Minimum PHP Version Packagist License

English | 中文

🔒 A framework-agnostic mutex library designed to facilitate serialized execution of PHP code in high-concurrency scenarios.

Requirements

  • PHP >= 7.1 or above
  • Redis >= 2.6.12 or above (required for distributed locks or Redlock)
  • Swoole >= 4.5 or above (required for coroutine-level mutex locks)

Installation

composer require pudongping/wise-locksmith

Quickstart

<?php

require 'vendor/autoload.php';

use Pudongping\WiseLocksmith\Locker;

$redisHosts = [
    [
        'host' => '127.0.0.1',
        'port' => 6379
    ],
    [
        'host' => '127.0.0.1',
        'port' => 6380
    ],
    [
        'host' => '127.0.0.1',
        'port' => 6381
    ],
    [
        'host' => '127.0.0.1',
        'port' => 6382
    ],
    [
        'host' => '127.0.0.1',
        'port' => 6383
    ],
];

// Initialize Redis instances if distributed locks or Redlock are needed; otherwise, you can skip this step
$redisInstances = array_map(function ($v) {
    $redis = new \Redis();
    $redis->connect($v['host'], $v['port']);
    return $redis;
}, $redisHosts);

// Create an instance of locker
$locker = new Locker();

Usage

An example of potential data inconsistency in high-concurrency scenarios is provided in the current project, see here.

flock - File Lock

File locking has no dependencies. You can set the lock's timeout using the optional third parameter, in seconds (supports floating-point numbers, e.g., 1.5 means 1500ms, which means it will wait for a maximum of 1500ms; if it fails to acquire the lock, it will actively release the attempt and throw a Pudongping\WiseLocksmith\Exception\TimeoutException exception). Setting it to Pudongping\WiseLocksmith\Lock\File\Flock::INFINITE_TIMEOUT means it never expires, and it will continuously block attempting to acquire the lock until successful. The default value is Pudongping\WiseLocksmith\Lock\File\Flock::INFINITE_TIMEOUT.

$path = tempnam(sys_get_temp_dir(), 'wise-locksmith-flock-');
$fileHandler = fopen($path, 'r');

$res = $locker->flock($fileHandler, function () {
    // Write the code you want to protect here
});

unlink($path);

return $res;

redisLock - Distributed Lock

Requires the redis extension. You can set the lock's timeout using the optional third parameter, in seconds (supports floating-point numbers, e.g., 1.5 means 1500ms, which means it will wait for a maximum of 1500ms; if it fails to acquire the lock, it will actively release the attempt and throw a Pudongping\WiseLocksmith\Exception\TimeoutException exception). The default value is 5. The fourth parameter is a unique value for the current lock, which is generally not needed to be set unless there are special circumstances.

$res = $locker->redisLock($redisInstances[0], 'redisLock', function () {
    // Write the code you want to protect here
});

return $res;

redLock - RedLock (Implementation of distributed locks in a Redis cluster environment)

The parameters required for setting up the redLock lock are identical to the redisLock lock, except for the first parameter. All other parameters are exactly the same. The redLock lock is a cluster implementation of the redisLock lock.

$res = $locker->redLock($redisInstances, 'redLock', function () {
    // Write the code you want to protect here
});

return $res;

channelLock - Coroutine-Level Mutex Lock

When using this lock, you need to have the swoole extension installed, and the version must be greater than or equal to 4.5. You can set the lock's timeout using the optional third parameter, in seconds (supports floating-point numbers, e.g., 1.5 means 1500ms, which means it will wait for a maximum of 1500ms; if it fails to acquire the lock, it will actively give up the attempt and return false to indicate the lock was not acquired). Setting it to -1 means it never expires, and it will continuously block attempting to acquire the lock until successful. The default value is -1.

$res = $locker->channelLock('channelLock', function () {
    // Write the code you want to protect here
});

return $res;

For all the mentioned locks, the business closure function is executed only after successfully acquiring the lock. It then returns the result of the business closure function's execution. Otherwise, if the lock is not acquired, the business closure function is not executed, and the return value is null.

Running tests

To run the test suite, clone this repository and then install dependencies via Composer.

composer install

Then, go to the project root and run:

php -d memory_limit=-1 ./vendor/bin/phpunit -c ./phpunit.xml.dist

or 

composer run test

Exception Handling

You can catch Pudongping\WiseLocksmith\Exception\WiseLocksmithException exceptions to capture all exceptions thrown by this library.

use Pudongping\WiseLocksmith\Exception\WiseLocksmithException;
use Pudongping\WiseLocksmith\Locker;

try {
    $locker = new Locker();
    // ...
} catch (WiseLocksmithException $exception) {
    var_dump($exception->getPrevious());
    var_dump($exception->getCode(), $exception->getMessage());
}

Acknowledgments

Contributing

Bug reports (and small patches) can be submitted via the issue tracker. Forking the repository and submitting a Pull Request is preferred for substantial patches.

License

MIT, see LICENSE file.

pudongping/wise-locksmith 适用场景与选型建议

pudongping/wise-locksmith 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.49k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「locking」 「lock」 「mutex」 「redlock」 「serialized」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pudongping/wise-locksmith 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pudongping/wise-locksmith 我们能提供哪些服务?
定制开发 / 二次开发

基于 pudongping/wise-locksmith 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-20