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

标签 php jquery mysql ajax wordpress

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

例如如果一个页面有 2000 像素的高度,它需要大约 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;

最佳答案

我很少考虑优化ajax请求,

这是我的代码:--

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插件...

关于php - 如何从缓存中优化ajax或请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22299714/

相关文章:

java - 如何编写 Web 应用程序 p2p 网络?

php - JavaScript 中是否有相当于 "new Array (number)"的 PHP?

jquery - Coffeescript 选择表行的 id

Javascript - 从动态创建的数组中删除特定元素

php - 数据库内容将自己设置为 0

PHP MySQL 查询 : match two tables with percentage match

javascript - jQuery 无法删除父 div

php - 使用 PHP PDO 显示 mysql select 语句

mysql - 在 mysql 中寻找 Toad for oracle 功能

c++ - 如何缓存从MySQL数据库创建的对象