简体   繁体   中英

Managing Multiple Web Pages with PHP

I'm creating a site with the use of PHP where a random number is generated (same number for everyone) and then stored in redis in order to grab the number for everyone on the web page so it's in-sync. However, I'm unsure the best way to proceed with this. As I'm generating and accessing Redis via PHP as well when a new number needs to be generated, which means that say if 10 people are on the webpage 10 random numbers would be generated and continuously overriding one another in the Redis.

So, my issue is how would I make this system only generate one number, and only that number is stored in Redis while all the other ones just grab the number from Redis?

EDIT: I understand I can check if the value already exists in Redis and if so not override it and just use that number, but, wouldn't that be extremely inefficient? If there is 10,000 users connected to that webpage that is 9,999 unnecessary checks with Redis.

Thank you.

You could just check if the key representing the number in Redis exists, and if it doesn't, then you could generate a new number. If you need to generate a new number at regular intervals (eg the random number is good for 5 minutes), you can set an expire time ( https://redis.io/commands/expire ) for the key holding the random number.

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