Code Monkey home page Code Monkey logo

lock's Introduction

环境要求

1.PHP >= 5.3
2.composer
3.redis
4.predis

composer 安装

移步 composer中文网.

redis 安装

redis不支持window平台, window平台下的redis服务器由微软团队维护,版本一般比较旧. 一些redis新数据结构和功能会有限制,建议window用户安装linux虚拟机

linux移步 redis中文网

window github redis window

predis 安装

composer require predis/predis

lock 安装

composer require nabao/lock

抢占锁

lock(callable $callback, string $lock_val, int $expiration = 60)

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

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

队列锁

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

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

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

使用

//静态调用
$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);

config配置

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

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

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.