Septharoth Posted November 19, 2014 Share Posted November 19, 2014 Basically, I've created a game serverlist in php. After some time, I've noticed in the console of two of my game servers I noticed it's spamming connections every time a user connects to the page(this happens a LOT, since people check the SLN often), it's been causing a bit of lag ingame when there is 10+ simultaneous connections from my webserver. Is there a way I can have the php code only execute every X minutes, so even if a user is connecting to the page, it will show the outputted data from the last check in? I'm using the fsockopen function to check the server's status, and other methods to query the server for information. This is the last thing I need to do for this project to be perfect, some help would be greatly appreciated ^^ Quote Link to comment Share on other sites More sharing options...
Solution gizmola Posted November 19, 2014 Solution Share Posted November 19, 2014 Yes there are many ways to do what you want. One of the simplest and at the same time, highly performant is to use a caching product. Seems like I've written volumes on this in the past, but the ones to look at are: APC (only if you're using php I'd probably recommend you start with Redis because it just has so many features that can come in handy, and it can use memory frugally. With the small amount of data you need to cache, it should be a very good fit for you. So-- in summary your code will become: 1. Request serverlist data from redis. 2. If found - display. (Coming right out of memory, this will be lightning fast) 3. If not found - get list of game servers. Store this data to cache with a time-to-live setting you find acceptable. You can experiment with this setting to see what the sweet spot is for you in terms of freshness of data vs. buffering the users. Start with 60 seconds probably. The only fly in the ointment (other than installation and configuration of redis on your server(s) is that you need a php client. Fortunately there are 2 options: a pure php version (predis) and a php extension written in C (phpredis). In your case, go with phpredis if you can, but that can become an issue if you don't have the environment or sysadmin experience to navigate pecl or any issues pecl might have compiling phpredis. I feel obligated to say that you can just dump this stuff in a "file" on the filesystem and use filesystem calls to look at the age of the file, and build your own cache that way as well, but that's old school and once you have redis available you may find that some of its other features enable you to quickly get new information up and running for your users. Quote Link to comment Share on other sites More sharing options...
Septharoth Posted November 19, 2014 Author Share Posted November 19, 2014 Best answer anyone's given me on the subject yet. Thank you so much, I figured I would have to cache some data, but wasn't entirely sure how to go about it. You more than solved this issue. THANK YOU! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.