服务器端Wordpress上的AJAX缓存

时间:2016-10-14 19:15:20

标签: jquery ajax wordpress

我需要在服务器端缓存AJAX响应,而不是每次客户端发出请求时都从数据库中获取数据。

function post_filter($filter_action,$filter_values) {
    var ajaxurl = SiteInfo.home_url+'/wp-admin/admin-ajax.php';
    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"action": $filter_action, filter_values: $filter_values},
        success: function(response) {
            jQuery("#post-filter-results").html(response);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

function post_filter($filter_action,$filter_values) {
    var ajaxurl = SiteInfo.home_url+'/wp-admin/admin-ajax.php';
    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        data: {"action": $filter_action, filter_values: $filter_values},
        success: function(response) {
            jQuery("#post-filter-results").html(response);
        }
    });
}

答案 1 :(得分:0)

如果你有一个php会话:

$_SESSION["your_key"] = your_response_object

示例(admin-ajax.php)伪代码:

if($_SESSION["your_key"])
{
   echo $_SESSION["your_key"];
}else{
   *call DB*
   $_SESSION["your_key"] = *db_response*;
   echo *db_response*;
}