如何从缓存中优化ajax或请求

时间:2014-03-10 12:10:01

标签: php jquery mysql ajax wordpress

我有来自亚马逊的云服务器,一次有大约20K的访问者,但问题是,我在我的网站上引入了一个部分,其内容取决于页面的高度。

e.g。如果一个页面的高度为2000px,它请求大约40个帖子,对于少量访问者来说是好的,但对于超过1000个访问者(在特定时间)不好。

当我激活它时,它停止了我的服务器并崩溃了mysql,那么有什么方法可以顺利发帖而不会失去访问者吗?

然后我使用

在5-5组中发帖
function sfpside_bar_post_request(sfpnumber_of_post) {
        iCountI = 1;
        numbverofrequest = Math.floor(sfpnumber_of_post / 3);
        remainderpost = sfpnumber_of_post % 3;
        spi = setInterval(function() {
            if (iCountI > numbverofrequest)
            {
                handle_sidepost(remainderpost,iCountI);
                clearInterval(spi);
            }else{
              handle_sidepost(3,iCountI);  
            }
            iCountI++;
        }, 1000);
    }
    function handle_sidepost(num_post,count) {
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        var data = {
            wp_nonce: '<?php echo wp_create_nonce('sfp_forum_secret_no_one_knows_LATEST_SIDEBAR'); ?>',
            action: 'sfp_sidebar_random_sidebar',
            post_num: num_post,
        };

        jQuery.ajax({
            'async': true,
            'type': 'POST',
            'url': ajaxurl,
            'data': data,
            'success': function(data)
            {
                var sidepost = jQuery.parseJSON(data);
                sidepostcontent = '<div id="sidesfppost'+count+'">';
                for (icount = 0; icount < sidepost.length; icount++) {
                    sidepostcontent += '<div class="sidebar-one-half"><div class="post_img">';
                    sidepostcontent += sidepost[icount].image;
                    sidepostcontent += '</div><article><div class="sflike">';
                    sidepostcontent += sidepost[icount].like;
                    sidepostcontent += '</div><div class="conten_wrap left"><h2>';
                    sidepostcontent += sidepost[icount].title;
                    sidepostcontent += '</h2></div></article><div class="snippet-box"><div class="left"><p class="sfp_greybtn">';
                    sidepostcontent += sidepost[icount].greybutton;
                    sidepostcontent += '</p></div><div class="right">';
                    sidepostcontent += sidepost[icount].circle;
                    sidepostcontent += '</div></div></div>';
                }
                sidepostcontent += "</div>";
                jQuery("#sfppost_sidebar_extra_post").append(sidepostcontent);
                jQuery(function() {
                    jQuery("#sidesfppost"+count+" img.imgload").css("width", "54px");
                    jQuery("#sidesfppost"+count+" img.imgload").css("height", "55px");
                    jQuery("#sidesfppost"+count+" img.imgload").lazyload({
                        load: image_loaded_page,
                        effect: "fadeIn",
                        failure_limit: 10,
                        threshold: 200
                    });
                });
            },
            contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15"
        });
    }

但它仍然崩溃我的MySql

我以前要求发布的WP

if ($_POST['action'] == 'sfp_sidebar_random_sidebar' && wp_verify_nonce($_POST['wp_nonce'], 'sfp_forum_secret_no_one_knows_LATEST_SIDEBAR')):

        $args = array('posts_per_page' => $_POST['post_num'], 'orderby' => 'rand', 'post_type' => 'sfp_forum');
        $rand_posts = get_posts($args);
        $iCount = 0;
        $postdata = array();
        foreach ($rand_posts as $post_data) :
            setup_postdata($post_data);
            $postdata[$iCount]['image'] = get_post_home_picture($post_data->post_content, $post_data->ID, $post_data->post_title, $author_username, 'category-small-thumb', 275, 416, true, true);
            $postdata[$iCount]['like'] = sfp_like_disklike_arrows("poplr", $post_data->ID, true);
            $postdata[$iCount]['title'] = "<a href='" . get_permalink($post_data->ID) . "'>" . sfp_strip_content($post_data->post_title, 50) . "</a>";
            $postdata[$iCount]['greybutton'] = sfp_get_grey_button($post_data->ID, $post_data->post_title, true);
            $postdata[$iCount]['circle'] = sfp_home_postCircle($post_data->ID, $post_data->comment_count, true);
            $iCount++;
        endforeach;
        wp_reset_postdata();
        echo json_encode($postdata);
    endif;
    die;

1 个答案:

答案 0 :(得分:0)

我很少考虑optimize ajax request

这是我的代码: -

var oldSearchData = new Array();
function handle_sidepost(num_post,count)
{
    var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    var data = {
            wp_nonce: '<?php echo wp_create_nonce('sfp_forum_secret_no_one_knows_LATEST_SIDEBAR'); ?>',
            action: 'sfp_sidebar_random_sidebar',
            post_num: num_post,
        };
    if(oldSearchData[num_post])
    {
        data = oldSearchData[num_post];
    } else {
        jQuery.ajax({
            'async': true,
            'type': 'POST',
            'url': ajaxurl,
            'data': data,
            'success': function(data)
            {
                oldSearchData[num_post] = data;
                //do your action.
            }
        });
    }
}

你可以在ajax数组中保存状态......

希望它对你有所帮助....

另一个解决方案是: - 使用WP Super Cache插件...