EchoFool Posted August 8, 2010 Share Posted August 8, 2010 Hey, Quick question on how i should improve my efficientcy.... Currently my site loads all the main variables with a query at the top of the page then the rest of the page does what it needs with the vars. But lets say when you page refresh - no data has altered from the main variables have been changed - but currently it still queries and assigns all the variables - but i find this inefficient if the data has not changed to keep on loading them every page load with a query. Is there more efficent way to make php only query and update teh already set variables when "something" has changed... although that also leads to the problem of variables not being set if no chance has happened when the page reloads....any thoughts? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 9, 2010 Share Posted August 9, 2010 Store the values in sessions, cookies etc. Cache pages using simple caching methods such as creating a copy of the file. It depends how dynamic your data is, how often it changes etc. Some pages might have different data each time, you just need to investigate the best option for you. Take a look at this article on capturing output using Output Buffering in PHP, this is a simple way to cache pages so you don't have to set everything again. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted August 9, 2010 Author Share Posted August 9, 2010 The global variables change quite regularly (multiple times a day) but some remain the same forever + 1. How secure is it when in a session - as some of the variables cannot be altered or may result in cheating my game. Also i have been looking into cache but have no idea where to learn, thanks for the link i will start there Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 9, 2010 Share Posted August 9, 2010 Well caching may or may not be for you. How are you loading your global variables? Are they from a MySQL database? MySQL is pretty efficient, it shouldn't bother it running the query every time. I guess it'll depend on the amount of traffic. But a lot of websites run multiple queries every page load and receive large amounts of visitors. Quote Link to comment Share on other sites More sharing options...
phil88 Posted August 9, 2010 Share Posted August 9, 2010 You could use APC to save the result of the query so that it doesn't have to be made on every page load, then whenever the table you're getting the global variables is updated just delete the cache so it doesn't have stale values and it'll rebuild with the new values on next page load. For example; // Getting the variables $cached = apc_fetch('global_vars'); if(!$cached){ // The variables aren't cached, so get them from the DB $query = "SELECT ... FROM global..."; $result = mysql_query($query); while($t = mysql_fetch_assoc($result)){ $cacheThis[] = $t; } apc_store('global_vars', $cacheThis); $useThis = $cacheThis; }else{ // The vars are cached, so just use from there $useThis = $cached; } // $useThis will have the variables you need // Then, where ever you have some code that updates the global values in the database, run this apc_delete('global_vars'); If your database is being updated so often that you're not getting many hits on the cache before the values change, it isn't really worth it. 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.