简体   繁体   English

使用PHP,AJAX和MySQL检查更新的最快方法

[英]Fastest way to check for update using PHP,AJAX and MySQL

I need to check for updates on a (max) one second interval for updates. 我需要检查(最大)一秒间隔的更新以进行更新。

I'm now looking for the fastest way to do that using AJAX for the requests and PHP and MySQL. 我现在正在寻找使用AJAX来处理请求以及PHP和MySQL的最快方法。

Solution 1 Every time new data, that needs to be retreived by other clients, is added to the MySQL database a file.txt is updated with 1. AJAX makes a request to a PHP file which will check if file.txt contains a 1 or 0. If it contains a 1 it will get the data from the MySQL database and return it to the client. 解决方案1每次需要由其他客户端检索的新数据添加到MySQL数据库时,file.txt将更新为1.AJAX向PHP文件发出请求,该文件将检查file.txt是否包含1或0.如果它包含1,它将从MySQL数据库获取数据并将其返回给客户端。

Solution 2 Every AJAX request calls a PHP file which will check directly into MySQL database for new data. 解决方案2每个AJAX请求都会调用一个PHP文件,该文件将直接检查MySQL数据库中的新数据。

Solution ..? 解决方案..? If there is any faster solution i'd be happy to know! 如果有更快的解决方案,我很乐意知道! (considering I can only use PHP/MySQL and AJAX) (考虑到我只能使用PHP / MySQL和AJAX)

Avoiding the database will probably not make the process significantly faster, if at all. 如果有的话,避免使用数据库可能不会显着加快流程。

You can use a comet-style ajax request to get near real-time polling. 您可以使用彗星式ajax请求来获得接近实时的轮询。 Basically, create an ajax request as usual to a php-script, but on the server side you poll the database and sleep for a short interval if there is nothing new. 基本上,像往常一样创建ajax请求到php脚本,但是在服务器端,你轮询数据库并在没有新内容的情况下休眠一小段时间。 Repeat until there is something of interest for the client. 重复,直到客户感兴趣。 If nothing appears within a timeframe of eg 60 seconds, close the connection down. 如果在例如60秒的时间范围内没有出现任何内容,请关闭连接。 On the client side, you only open a new connection once the first has terminated (either with a response or as a timeout). 在客户端,只有在第一个连接终止时才会打开新连接(无论是响应还是超时)。

See: https://en.wikipedia.org/wiki/Comet_(programming) 请参阅: https//en.wikipedia.org/wiki/Comet_ (programming)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM