当前位置:网站首页>PHP解决并发问题的几种实现
PHP解决并发问题的几种实现
2022-07-25 17:36:00 【kankan231】
对于商品抢购等并发场景下,可能会出现超卖的现象,这时就需要解决并发所带来的这些问题了
在PHP语言中并没有原生的提供并发的解决方案,因此就需要借助其他方式来实现并发控制。
方案一:使用文件锁排它锁
flock函数用于获取文件的锁,这个锁同时只能被一个线程获取到,其它没有获取到锁的线程要么阻塞,要么获取失败
在获取到锁的时候,先查询库存,如果库存大于0,则进行下订单操作,减库存,然后释放锁
方案二:使用Mysql数据库提供的悲观锁
Innodb存储引擎支持行级锁,当某行数据被锁定时,其他进程不能对这行数据进行操作
先查询并锁定行:select stock_num from table where id=1 for update
if(stock_num > 0){
//下订单
update table set stock_num=stock-1 where id=1
}
方案三:使用队列
将用户的下单请求依次存入一个队列中,后台用一个单独的进程处理队列中的下单请求
方案四:使用Redis
redis的操作都是原子性的,可以将商品的库存存入redis中,下单之前对库存进行decr操作,如果返回的值大于等于0等可以下单,否则不能下单,这种方式效率较高
if(redis->get('stock_num') > 0){
stock_num = redis->decr('stock_num')
if(stock_num >= 0){
//下订单
}else{
//库存不足
}
}else{
//库存不足
}
其他并发问题:
在现实应用中,很多情况下会把数据存入缓存,当缓存失效时,去数据库取数据并重新设置缓存,如果这时并发量很大,会有很多进程同时去数据库取数据,导致很多请求
穿透到数据库,而使数据库奔溃,这里可用文件锁来解决
$data = $cache->get('key');
if(!$data){
$fp = fopen('lockfile');
if(flock($fp, LOCK_EX)){
$data = $cache->get('key');//拿到锁后再次检查缓存,这时可能已经有了
if(!$data){
$data = mysql->query();
$cache->set('key', $data);
}
flock($fp, LOCK_UN);
}
fclose($fp);
}说白了,要解决并发问题就必须要加锁,各种方案的本质都是加锁
边栏推荐
- 【硬件工程师】DC-DC隔离式开关电源模块为什么会用到变压器?
- 霸榜COCO!DINO: 让目标检测拥抱Transformer
- Redis cluster deployment based on redis6.2.4
- 面试官:说说 log.Fatal 和 panic 的区别
- pgsql有没有好用的图形化管理工具?
- 对灰度图像的三维函数显示
- postgreSQL 密码区分大小写 ,有参数控制吗?
- Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
- Crawler framework crawler
- Chapter VI succession
猜你喜欢

Redis源码与设计剖析 -- 16.AOF持久化机制

哈夫曼树的构建

An article about ultrasonic humidifier

EDI 对接CommerceHub OrderStream

How to prevent the unburned gas when the city gas safety is alarmed again?

食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!

计算日期或日期格式化

Using rank to discuss the solution of linear equations / the positional relationship of three planes

Redis源码与设计剖析 -- 17.Redis事件处理

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
随机推荐
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
一篇文章了解超声波加湿器
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
window10系统下nvm的安装步骤以及使用方法
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
Is it safe to open a futures account online? How to apply for a low handling fee?
11、照相机与透镜
We were tossed all night by a Kong performance bug
Page table cache of Linux kernel source code analysis
ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
我也是醉了,Eureka 延迟注册还有这个坑!
交叉验证(cv)学习笔记
Boring post roast about work and life
I'm also drunk. Eureka delayed registration and this pit!
How to install govendor and open a project
02.两数相加
Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
爬虫框架-crawler
I2C communication - sequence diagram