定制 pizsd/hyperf-redis-lock 二次开发

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

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

pizsd/hyperf-redis-lock

最新稳定版本:v1.1.2

Composer 安装命令:

composer require pizsd/hyperf-redis-lock

包简介

hyperf3-redis-lock

关键字:

README 文档

README

English | 中文

an easy redis-based distributed-lock implementation for hyperf 2.*。 This extension features distributed-lock includes block and unblock distributed-lock.

Principle

The command in Redis is atomic.so we use set to make sure that your application executes sequentially. The Redis version before 2.8 does't support the option ex in set command. so we just use setnx+expire instead.>> Version 1.* The Redis version after 2.8 supports the option ex in set command. so we use set nx ex>> Version 2.*

so version 2.* supports the version of redis after 2.8. version 1.* supports all the versions of redis.

Redis version

redis-server --version

Install

Run composer require pizsd/hyperf-redis-lock

Usage

init the redis object

    #[Inject]
    protected Redis $redis;

    public function __construct(RedisFactory $redisFactory)
    {
        $this->redis = $redisFactory->get('default');
    }
  • non-block lock we try to acquire the lock then return the result directly.If acquire the lock, execute the Closure and return the Closure's result. or return false;
public function lock(ResponseInterface $response)
{
    $lock = new RedisLock();
    $res = $lock->get(lock, 20 *100, function () {
        sleep(10);
        return [123];
    });
    return $response->json($res);
}
  • blocking lock we try acquire the lock first. 1.if failed we try to acquire the lock every 250 ms util timeout(the time we waiting for is bigger than the expire time you set in your application) 2.if success then execute the closure and return the result of closure.
    Tips: if timeout occurs in the process of application you need to catch the exception LockTimeoutException to handle this situation. for example:
/**
 * @return \Psr\Http\Message\ResponseInterface
 */
public function lockA(ResponseInterface $response)
{
    try {
        $lock = new RedisLock();
        $res = $lock->block('lock', 44, function () {
            return [456];
        });
        return $response->json(['res' => $res]);
    // catch the exception
    } catch (LockTimeoutException $exception) {
        var_dump('lockA lock check timeout');
        return $response->json(['res' => false, 'message' => 'timeout']);
    }
}

Finally

Contributing

Feel free to create a fork and submit a pull request if you would like to contribute.

Bug reports

Raise an issue on GitHub if you notice something broken.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固