I have the following code:
<div id="uniquefriends">
<?php
$friends = $helper->getuniqueUser($uid, $TOKEN);
foreach($friends as $friend) {
echo $friend; // Facebook UID
}
?>
</div>
<a href="#" id="other"><img src="bilder/arrow_otherfriends.png" style="height: 30px;" alt="load other friends" /></a>
<script>
$(document).ready( function() {
$('#other').click(function() {
$.ajax({
type: 'POST',
url: 'otherfriends/',
data: { uid: "<?php echo $uid; ?>", token: "<?php echo $TOKEN; ?>" },
cache: false,
success: function(result) {
$('#uniquefriends').html(result);
},
});
});
});
</script>
Description:
$friends = $helper->getuniqueUser($uid, $TOKEN);
is getting all the friend of the user /me/ and compares it with the DB and array_diff() shows me the difference of the friends with the users that already using the app.
You can imagine, thatcalling this function will generate a huge overload, so it would be clever not to call it multiple times.
This function gives me just 4 uid's per call (array_slice($array, 0, 4)) (Can modify that)
Problem:
When a user clicks on "<a href="#" id="other"><img src="bilder/arrow_otherfriends.png" style="height: 30px;" alt="load other friends" /></a>
" the next set of users will be generated and given me out as the result (see ajax call).
The ajax call looks like this:
<?php
if(isset($_POST['uid']) && !empty($_POST['uid'])) {
$uid = $_POST['uid'];
$token = $_POST['token'];
$helper = new helper();
$friends = $helper->getuniqueUser($uid, $token);
foreach($friends as $friend) {
echo $friend;
}
}
?>
So I'm calling every time "$friends = $helper->getuniqueUser($uid, $TOKEN);"
when a user clicks the link. The performance goes down and the waiting time is very long.
Can I somehow Improve this code, to NOT calling this function so often? Are there any options?
You can implement a little cache system in the PHP script that get called by your AJAX.
every time you call getUniqueUser
store somewhere (session, files, database, a mix of them) the result, paired with the input value(s).
If the input value(s) is(are) already known in the cache, just fetch the result from it and give it back instantly, otherwise, launch the function and store the missing result in your cache.
Of course you may want a sophisticated cache, tracking the timestamp of the request to avoid returning "stale" friends (maybe you added new friends recently and you need a renew of the cache). It's just a starting point, but if you don't want to call the same function all the time, you need some form of caching.
If
$friends = $helper->getuniqueUser($uid, $token);
Is indeed fetching all related users (instead of walking through all of the friends, with a function call for each related friend, which makes no sense of course), then the overload should not be as much as you speak of.
However, if you really find that the server takes a long time to answer your response, then there are a few options.
Not-so-maybe-related: please don't mix javascript and php together, It's simply a bad habit. Just as much as you should avoid mixing too much php and html together.
I believe you do need only fetch list one time, so add a class in the image tag. Trigger request only once. There is no meaning to fetch result in every click, because you only send same parameters.
$('#other').click(function() {
if (!$(this).hasClass('clicked')) {
$.ajax({
type: 'POST',
url: 'otherfriends/',
data: { uid: "<?php echo $uid; ?>", token: "<?php echo $TOKEN; ?>" },
cache: false,
success: function(result) {
$('#uniquefriends').html(result);
$('#other').addClass('clicked');
}
});
} });