search
HomeBackend DevelopmentPHP Tutorial大量过期订单的处理问题

  1. 描述你的问题
    最近开发遇到一个大量订单过期处理的问题,比如我拍了一个商品,半个小时之后他就不能付款了或者这个订单应该被删除掉。

第一、反应是crontab,但是考虑到每次都要去查mysql,感觉会影响效率;
第二、有人提示我用消息队列rabbitmq,但是我看了半天感觉消息队列好像没啥用,我感觉逻辑还是拍下订单的时候发送消息,然后php进程一直在那sleep?估计是我理解的不够透彻,求大神解释下相关流程。
第三、还有其他方法么?大神给个具体点的方案撒~

  1. 贴上相关代码

  2. 贴上报错信息

  3. 贴上相关截图

  4. 已经尝试过哪些方法仍然没解决(附上相关链接)

回复内容:

  1. 描述你的问题
    最近开发遇到一个大量订单过期处理的问题,比如我拍了一个商品,半个小时之后他就不能付款了或者这个订单应该被删除掉。

第一、反应是crontab,但是考虑到每次都要去查mysql,感觉会影响效率;
第二、有人提示我用消息队列rabbitmq,但是我看了半天感觉消息队列好像没啥用,我感觉逻辑还是拍下订单的时候发送消息,然后php进程一直在那sleep?估计是我理解的不够透彻,求大神解释下相关流程。
第三、还有其他方法么?大神给个具体点的方案撒~

  1. 贴上相关代码

  2. 贴上报错信息

  3. 贴上相关截图

  4. 已经尝试过哪些方法仍然没解决(附上相关链接)

低成本解决方案

插入订单的时候同时插入过期的时间

然后服务端用cli模式不断查询mysql数据库就行啊

建立mysql过期时间的索引 并不会影响效率

如果对运营成本有要去的话,可以通过用户触发模式来实现:

  1. 在拍完商品的同时设定"过期时间" datetime。

  2. 用户对该商品的任何更新都要同时更新"过期时间"。

  3. 每次用户登录后,读取相关商品的时候通过php对比当前时间(如果对时间要求严格,建议用JS对比时间)跟商品过期时间。如果过期了,可以通过css标识当前商品已经过期。同事发送请求到rabbitmq队列,因为读取商品的时候已经得到了该产品的ID,所以最好的列队信息是要包括这个ID。

  4. 后台有crontab定时读取rabbitmq并且根据已知的ID批量生产删除的mysql语句。

这种处理方法可以节省资源,减低无服务不必要的消耗,基本上就是一次读取,一次删除的数据库操作而已。非常不建议逐条删除,也建议逐条搜索mysql找出过期条目,最好是根据rabbitmq队列生产批量删除语句。

可以把订单存进redis 给redis设置过期时间

过期时间加索引,然后定时任务去处理,每次更新固定条数就好了。比如定时脚本每次启动,根据条件去查询,每次查1000条,更新状态为过期未付款,如果结果不足1000条或是没有结果,就说明不用查下一次了。

你的问题是不是拍了后半小时没付款就删除,如果是的话,以下几点思路你可以看看

  1. nosql,需要持久化的话就用redis,拍下就设定过期时间,到时间后自动删除(这种情况就适合用nosql特性),你前台程序需要进行判断。
    2.如果不允许用redis的话(就是用mysql类关系型数据库咯),也是拍下的时候设定过期时间戳,然后在订单相关页面手动进行是否过期(如果过期时间和订单在一个表,不会浪费资源啊,取多一个字段而已)

是我的理解错了吗,为什么需要crontab和消息队列??

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),