2.0.0 • Published 3 years ago

egg-locker v2.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

egg-locker

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-locker --save

Usage

// {app_root}/config/plugin.js
exports.locker = {
  enable: true,
  package: 'egg-locker',
};

Configuration

// {app_root}/config/config.default.js
exports.locker = {
    //redisName: 'lock',//egg-redis的实例名称,多实例的时候需要配置
    errorMessage: '系统繁忙,请稍后再试',
    maxListenerCount: 2000,
    keyPrefix:'lk:'
};

see config/config.default.js for more detail.

Example

// app/controller/index.js
const BaseController = require('egg').Controller

class Controller extends BaseController {
    async home(){
        const orderid = 'xxxxx'
        //并发控制的方法,相同的orderid只有一个调用者能获得锁并进入,其他调用者会等待锁的释放
        await this.ctx.runInLock(orderid,async ()=>{
            await doSomething()
        })
    }
}

Questions & Suggestions

Please open an issue here.

License

MIT