简体   繁体   中英

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.

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. 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.

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. 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; 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?

EDIT: My host doesn't have memcached available for me to use and I cannot install it. 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 ?

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".

You can read more about memcached at its main web site , or simply use the links above to investigate PHP's modules.

For something like this you want to use a technique known as 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. This allows you to write to the connection immediately.

Google for "Comet" and "PHP" and you should find some good resources. http://www.zeitoun.net/articles/comet_and_php/start looks thorough.

http://memcached.org/ is what your looking for. This is directly made for fast ram based data access to objects, arrays, and variables in your system. Cuts out the load on MySQL as long as your doing concurrent updates.

If you're not locking, just reading, "100+" requests isn't even that heavy. Have you considered just doing a stress test?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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