猿问

lnmp高并发情况下出现TP5中的函数重复执行

1.
问题描述
lnmp高并发情况下出现TP5中的函数重复执行
问题出现的环境背景及自己尝试过哪些方法
已经使用cache作为判断标准,限制一段时间内就算函数执行,也保证数据不会入库。
相关代码
php代码:
publicfunctionwinprize()
{
if(Request::instance()->isPost()){
$openid=Cookie::get('hzz_openid');
$win_type=db('winprize')->where('openid',$openid)->where('state',0)->field('type')->find();
if($win_type){
if($win_type['type']==0){
returnjson(['code'=>4,'msg'=>'存在记录']);
}else{
returnjson(['code'=>5,'msg'=>'不存在的记录']);
}
}
$user_id=input('id');
$user=db('user')->where('id',$user_id)->find();
if($user['number']<$user['max_number']){
if($user['max_number']>2){
$fact_assist=db('assist')->where('openid_ass',$openid)->count();
if($fact_assist<(5*$user['max_number'])){
db('assist')->where('openid_ass',$openid)->update(['max_number'=>floor($fact_assist/5)]);
returnjson(['code'=>2,'msg'=>'无效的']);
}
}
$jackpot=db('jackpot')->where('state',1)->find();
$data['jackpot_id']=$jackpot['id'];
$data['user_id']=$user['id'];
$data['openid']=$user['openid'];
$data['create_time']=time();
if($jackpot['type']==0){
if(!empty($user['redpack_openid'])){
$data['redpack_openid']=$user['redpack_openid'];}
$data['price']=$jackpot['price'];
}else{
$data['type']=$jackpot['type'];}
Db::startTrans();
try{
db('jackpot')->where('id',$jackpot['id'])->update(['state'=>0]);
db('user')->where('openid',$openid)->update(['number'=>$user['number']+1]);
db('winprize')->insert($data);
Db::commit();
returnjson(['code'=>0,'type'=>$jackpot['type']]);
}catch(\Exception$e){
Db::rollback();
returnjson(['code'=>3,'msg'=>'事务出现异常!']);}
}else{
returnjson(['code'=>1,'msg'=>'没有抽奖机会']);}
}
returnjson(['code'=>-1,'msg'=>'错误']);
}
nginx.conf:
userwwwwww;
worker_processes16;
error_log/home/wwwlogs/nginx_error.logcrit;
pid/usr/local/nginx/logs/nginx.pid;google_perftools_profiles/tmp/tcmalloc;
events
{
useepoll;
worker_connections65535;
multi_accepton;
}
http
{
includemime.types;
default_typeapplication/octet-stream;
server_names_hash_bucket_size128;
client_header_buffer_size32k;
large_client_header_buffers432k;
client_max_body_size50m;
sendfileon;
tcp_nopushon;
keepalive_timeout60;
tcp_nodelayon;
fastcgi_connect_timeout300;
fastcgi_send_timeout300;
fastcgi_read_timeout300;
fastcgi_buffer_size64k;
fastcgi_buffers464k;
fastcgi_busy_buffers_size128k;
fastcgi_temp_file_write_size256k;
gzipon;
gzip_min_length1k;
gzip_buffers416k;
gzip_http_version1.1;
gzip_comp_level2;
gzip_typestext/plainapplication/javascriptapplication/x-javascripttext/javascripttext/cssapplication/xmlapplication/xml+rss;
gzip_varyon;
gzip_proxiedexpiredno-cacheno-storeprivateauth;
gzip_disable"MSIE[1-6]\.";
#limit_conn_zone$binary_remote_addrzone=perip:10m;
##Ifenablelimit_conn_zone,add"limit_connperip10;"toserversection.
server_tokensoff;
access_logoff;
server
{
listen80default_server;
#listen[::]:80default_serveripv6only=on;
server_name_;
indexindex.htmlindex.htmindex.php;
root/home/wwwroot/default;
#error_page404/404.html;
#DenyaccesstoPHPfilesinspecificdirectory
#location~/(wp-content|uploads|wp-includes|images)/.*\.php${denyall;}
includeenable-php.conf;
location/nginx_status
{
stub_statuson;
access_logoff;
}
location~.*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires30d;
}
location~.*\.(js|css)?$
{
expires12h;
}
location~/.well-known{
allowall;
}
location~/\.
{
denyall;
}
access_log/home/wwwlogs/access.log;
}
includevhost/*.conf;}
查阅了一下nginx与php错误日志,没有发现有异常记录。请大神帮忙分析一下
慕侠2389804
浏览 356回答 2
2回答

米琪卡哇伊

使用redis的分布式锁就可以解决了,缓存解决不了问题同一时刻取请求缓存都会查不到缓存,导致并发redisset命令可以传入NX和EX来解决并发问题
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答