基于tp的redis cache实现的简易锁及相关应用

浏览:1275 发布日期:2018/07/26 分类:技术分享 关键字: cache lock captcha sms
https://github.com/he426100/think-lock
在项目开发过程中经常会碰到需要防止同一个用户并发请求的情况,比如财务结算时需要保证结算过程不受干扰,除了数据库锁之外,还可以使用Cache来实现锁功能,即在过程结束前不允许有新的执行。

示例一:$lock = new Lock('login_'.$param['name'], 30);
if (!$lock->lock()) {
    $this->restError('请30s后在尝试');
}
Lock类第一个参数是锁名,第二个是锁定时间(默认永久)

示例二:$mobile = input('param.mobile');
$sms = new Sms($mobile);
if (!$sms->canSend()) {
    return $this->restResponse(10046);
}
if (($result = $sms->send()) === true) {
    return $this->restResponse(['msg' => $sms->getResult()]);
}
return $this->restResponse($result == '2' ? 10046 : 10011);
$sms = new Sms($params['username']);
if (!$sms->verifyCode($params['captcha'])) {
    return $this->restResponse('10010');
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>