douchen2025 2013-04-23 10:05
浏览 43

Jquery Ajax / PHP - 多次调用函数

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?

  • 写回答

3条回答 默认 最新

  • douzhang5121 2013-04-23 10:16
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?