Git Product home page Git Product logo

lock's Introduction

laravel版本

环境要求

1.PHP >= 7
2.composer
3.redis(必须支持lua)
4.predis

composer 安装

移步 composer中文网.

redis 安装

redis中文网

predis 安装

composer require predis/predis

lock 安装

composer require nabao/lock

抢占锁

lock(callable $callback, string $lock_val)

多进程并发时, 其中某一个进程得到锁后, 其他进程将被拒绝

$callback  
            回调函数, 可返回值
$lock_val
            锁定值

多参数抢占锁

lock(callable $callback, array $lock_vals)

多进程并发时, 其中某一个进程得到锁后, 其他进程将被拒绝

$callback  
            回调函数, 可返回值
$lock_vals
            锁定值(数组)

队列锁

queueLock($closure, $lock_val, $max_queue_process = 100, $expiration = 60)

多进程并发时, 其中某一个进程得到锁后, 其他进程将等待解锁(配置最大等待进程后, 超过等待数量后进程将被拒绝)

$callback  
                回调函数, 可返回值
$lock_val
                锁定值
$max_queue_process        
                队列最大等待进程        
$expiration
                进程最大执行时间   

多参数队列锁

queueLock($closure, $lock_vals, $max_queue_process = 100)

多进程并发时, 其中某一个进程得到锁后, 其他进程将等待解锁(配置最大等待进程后, 超过等待数量后进程将被拒绝)

$callback  
                回调函数, 可返回值
$lock_vals
                锁定值(数组)
$max_queue_process        
                队列最大等待进程       

使用

//静态调用
$lock_val = 'user:pay:1';
Lock::lock(function($redis){
   echo 'hello world!';
}, $lock_val);
        
//实例化调用
$lock = new Lock();
$lock_val = 'user:pay:1';
$lock->lock(function($redis){
    echo 'hello world!';
}, $lock_val);

//多参数锁
$lock = new Lock();
$lock_val[] = 'user:pay:1';
$lock_val[] = 'user:pay:2';
$lock->lock(function($redis){
    echo 'hello world!';
}, $lock_val);

限流

isActionAllowed($key, $period, $max_count)

$key        限制key
$period     限制时间(秒)
$max_count  限制时间内最大数量

config配置

目前兼容tp.其它框架请实例化传参

 /*
    |--------------------------------------------------------------------------
    | lock配置文件
    |--------------------------------------------------------------------------
    |
    |drive 锁驱动(默认redis)
    |
    |redis redis驱动配置
    |   host 地址
    |   port 端口
    |
    |params 参数配置
    |   max_queue_process  进程池最大进程
    |   expiration         锁值过期时间
    |
    */
    'lock'=>[
        'drive' =>  'redis',
        'redis' =>  [
            'host'  =>  '127.0.0.1',
            'port'  =>  '6379'
        ],
        'params' => [
            'max_queue_process' => 100
            'expiration'        =>  5
        ]
    ]

lock's People

Contributors

userzhangjinlong avatar zhaocong6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lock's Issues

在进行实例化自定义配置的时候,没有正确加载到配置

Factory.php文件下
private function getConfig($config = [])
{
if ($this->config) return $this->config;

    //判断是否是实例化传值
    if (!empty($config)) {
        $config = array_merge($this->default_config, $config);
        $this->drive = $config['drive'];
         #此处源代码加载了错误的选项$this->config =$config['drive'];
        return $this->config = $config;
    }

    //判断是否是tp框架
    if (defined('THINK_VERSION')){
        $this->drive  = C('lock')['drive'];
        $this->config = C('lock')[$this->drive];
    }

    //设置默认参数
    if (empty($this->config)){
        $config      = $this->default_config;
        $this->drive = $config['drive'];
    }

    return $this->config;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.