2

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?

Marek123
  • 1,193
  • 7
  • 35
  • 75

3 Answers3

2

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.

STT LCU
  • 4,348
  • 4
  • 29
  • 47
  • I now store the generate friend uid in a json file and call the file instead of calling the function. It's much faster now. Now I have to think about a caching system for the file. – Marek123 Apr 23 '13 at 10:34
  • @Marek123 glad to have helped. remember to upvote and accept the answer you find the most useful – STT LCU Apr 23 '13 at 12:02
1

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.

  1. Optimize your database, how does it look like? Is it using indexing properly and not using circular dependencies?
  2. Don't select * from the database if you really just want 4 results each time, use LIMIT.
  3. Consider storing the information in a text file, you can easily fetch data from the database ONCE per user login, create an array with the information and then you can parse the array within the file when you have to show more users, instead of query-ing the database again.
  4. Take advantage of the cache.

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.

Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • The database will be just called once within the function call to get the users, that are already using the app. In the moment just approx. 10 entries. But the DB has all optimization we could do. We do not select * from the DB. - The function is calling the graphi api all the time to get the friends of the user. That is generating the overload. – Marek123 Apr 23 '13 at 10:21
  • I see, then you should query the API once and store the information elsewhere like I mentioned, that way you will stay away from the overload. I can't think of better ways right now. Do you understand what I mean? – Jonast92 Apr 23 '13 at 10:24
  • I did it now like here: http://stackoverflow.com/questions/2662268/php-how-do-i-store-an-array-in-a-file-to-access-as-an-array-later-with-php It's much faster than before. But I'm worried about the "files" that are stored. Do I have to delete them after a while? What do you think? – Marek123 Apr 23 '13 at 10:33
  • You could delete them once the user logs out. I'm sure there are plenty of discussion about file management under these circumstances. But please up vote if the answer is helpful. You should also consider upvoting STT LCU's answer if it was helpful to you. – Jonast92 Apr 23 '13 at 10:39
-1

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');
                  }
              });
            } });