简体   繁体   English

如何存储数百个用户每秒可以访问的PHP变量?

[英]How to store a PHP variable that hundreds of users can access every second?

Currently, I have a website that people can open up during a certain team's hockey games. 目前,我有一个人们可以在某个团队的曲棍球比赛中打开的网站。 When the hockey team scores, a designated person clicks a button in a secure location. 当曲棍球队得分时,指定人员点击安全位置的按钮。 This updates a single entry in MySQL database with the current timestamp. 这将使用当前时间戳更新MySQL数据库中的单个条目。

On the front-end of the website, there is an asynchronous call that runs every 15 seconds to a PHP script to query the database for that timestamp. 在网站的前端,有一个异步调用,每隔15秒运行一次PHP脚本,以查询数据库中的时间戳。 The script then compares the current time to the timestamp pulled and if it's within 15 seconds of the current timestamp, it triggers an event on the webpage that includes playing the sound of an air horn and playing a short clip of the team's goal song. 然后脚本将当前时间与拉出的时间戳进行比较,如果它在当前时间戳的15秒内,它会在网页上触发一个事件,包括播放一个空气喇叭的声音并播放团队目标歌曲的短片。

I usually get a good amount of traffic to the sight during the team's games, however many people complain about the (up to) 15 second delay after the goal is scored for the sound to be triggered. 在球队的比赛中,我通常会获得大量的视力,但是很多人抱怨在触发声音的目标得分后(最多)15秒的延迟。 I'd like to find a way to remedy that. 我想找到一种方法来解决这个问题。

Obviously, I don't think querying the database every single second for every single users who is on the page (think 100+) is going to work; 显然,我不认为对于页面上的每个用户(想想100+)每一秒都要查询数据库; I'll likely kill my database. 我可能会杀了我的数据库。 So, is there another way I can achieve my result? 那么,还有另一种方法可以实现我的结果吗? Would it be possible to place a PHP variable into the server's memory that can be pulled by each session without the negative consequences as using a database or file system read? 是否可以将PHP变量放入服务器的内存中,每个会话都可以将其拉出而不会产生使用数据库或文件系统读取的负面后果?

EDIT: My host doesn't have memcached available for me to use and I cannot install it. 编辑:我的主机没有memcached可供我使用,我无法安装它。 It's disappointing because that sounds like it would have been the optimal solution. 这令人失望,因为这听起来像是最佳解决方案。 Does anyone have an alternative idea I could look in to that doesn't use memcached ? 有没有人有一个我可以看到的另一个想法,不使用memcached

In this situation, something like memcached (also available in an objectified form as memcache ) is most likely the perfect solution, one of its design goals being "to decrease database load in dynamic web applications". 在这种情况下,类似memcached (也以对象形式提供为memcache )之类的东西很可能是完美的解决方案,其设计目标之一是“减少动态Web应用程序中的数据库负载”。

You can read more about memcached at its main web site , or simply use the links above to investigate PHP's modules. 您可以在其主要网站上阅读有关memcached的更多信息,或者只是使用上面的链接来研究PHP的模块。

For something like this you want to use a technique known as Comet. 对于这样的事情,你想使用一种称为Comet的技术。 Its not particularly difficult, but requires a bit of effort. 它并不是特别困难,但需要一点努力。

Basically you'll keep a live connection open to each of the browsers, instead of having them re-open the connection every 15 seconds. 基本上,您将为每个浏览器保持一个实时连接,而不是让它们每15秒重新打开一次连接。 This allows you to write to the connection immediately. 这允许您立即写入连接。

Google for "Comet" and "PHP" and you should find some good resources. 谷歌的“彗星”和“PHP”,你应该找到一些很好的资源。 http://www.zeitoun.net/articles/comet_and_php/start looks thorough. http://www.zeitoun.net/articles/comet_and_php/start看起来很彻底。

http://memcached.org/ is what your looking for. http://memcached.org/正是您的需求。 This is directly made for fast ram based data access to objects, arrays, and variables in your system. 这直接用于快速基于ram的数据访问系统中的对象,数组和变量。 Cuts out the load on MySQL as long as your doing concurrent updates. 只要您进行并发更新,就可以减轻MySQL的负担。

If you're not locking, just reading, "100+" requests isn't even that heavy. 如果您没有锁定,只需阅读,“100+”请求甚至不会那么重。 Have you considered just doing a stress test? 你考虑过做压力测试吗?

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

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