Jump to content

Efficientcy on variables


EchoFool

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.