抽奖锁

        $redis = cmsRedis::getInstance();
        $lockKey = '';
        if(cmsRedis::$isConnect){
            // 抽奖锁,验证同一时间最大抽奖数量限制
            $limitKey = sprintf(cmsRedis::Pre_TurntableMobileLock,'*');
            $limitKeyList = $redis->keys($limitKey);
            if(count($limitKeyList) >= $this->drawLimit){
                $data['msg'] = '服务器繁忙,请稍后再试。';
                $this->ajax_return1($data, 'Busy Server', 2);
            }
            // 抽奖锁,同时只接受一个抽奖的提交     
            $mobile = (string)$mobile;
            $lockKey = sprintf(cmsRedis::Pre_TurntableMobileLock,$mobile);
            $lockValue = rand(0,99999999);
            $redis->setnx($lockKey,$lockValue);
            $setLockValue = $redis->get($lockKey);
            if($setLockValue != $lockValue){
                $data['msg'] = '请勿多次提交.';
                $this->ajax_return1($data, 'Invalid Request', 2);
            }
            $redis->expire($lockKey,60);
        }

 目的:同一时间最大抽奖数量限制为50

代码设计:

1.定义模式$key='turntable::*'

2.抽奖开始,将信息存入redis,key='turntable::手机号',值随机数,抽奖结束释放该键

3.用户抽奖前,读取匹配该模式的键数量,小于50允许执行,大于50返回"系统繁忙"

原文地址:https://www.cnblogs.com/xiaobiaomei/p/8384913.html