正在延迟将记录添加到数据库中


Record is being added to the DB with delay

我使用Cakephp(在开发模式下),我的数据源是数据库/Mysql。

在我的系统中,我有以下过程:

  1. 访问者选择6个项目
  2. 访客点击按钮
  3. 点击按钮后,我使用jquery$.post将数据发送到一个php函数,该函数接受该数据并将其添加到数据库中
  4. 在那之后(或者可能不是看起来的那样),他们的访问者被重定向到"支付页面"

这是负责将数据发送到php函数"handle"的jquery脚本

$('.next-step').on('click', function(){
    var paymentPage = $(this).attr('data-href');
    var slotsData = [];
    var i = 0;
    $('.slot').each(function(){
        slotsData[i] = {
            'img-id': $(this).attr('data-id'),
            'img-src': $(this).attr('data-src'),
            'img-high-src': $(this).attr('data-high-src'),
            'img-page': $(this).attr('data-page')
            };
        i++;    
    }); 
    var setid = 0;
    $.post('/sets/handle', {k:'',setid:setid,setData:slotsData}, function(data){
                window.location = paymentPage;
    });
    return true;
});

这是php函数句柄

public function handle()
{
    $this->autoRender = false;
    $this->request->onlyAllow('ajax');
    $set_id = (int)$this->request->data('setid');
    $images = serialize($this->request->data('setData'));

    //Key
    $cookie_key = $this->Cookie->read('visitor_key');
    if(isset($cookie_key) && $cookie_key != null)
    {
        $key = $cookie_key;
    }
    else
    {
        $key = $this->newKey();
        $this->Cookie->write('visitor_key', $key);
    }

    //Edit Mode
    if(isset($set_id) && $set_id > 0)
    {
        /*Some other irrelevant code */         
    }
    else //Creating a new set
    {
        $this->Set->create();
        $this->Set->save(array(
            'images' => $images,
            'visitor_key' => $key,
            'quantity' => 1
        ));
        AppController::logActivity( array('visitor_key' => $key, 'instagram_id' => 0, 'instagram_username' => ''), 'Created New Set', $this->Set->id );
        return json_encode(array('status' => 'created','setid' => $this->Set->id,'visitor_key'=>$key));
    }

}

这是php函数支付

public function payment() {
    $this->set('title_for_layout', 'Review Your Order');
    /*
    debug( date("d-m-y H:i:s") );
    */                  
    $sets = $this->Set->findAllByVisitorKeyAndOrderId($this->Cookie->read('visitor_key'),'0');
    /*
    debug($sets);
    */      
    $onesetcost = Configure::read('Ecom.setcost');
    $visitor_key = $this->Cookie->read('visitor_key');
    $total_sets = 0;
    foreach($sets as $set){
        $total_sets += $set['Set']['quantity'];
    }
    $shippmentCost = $this->shippmentCostByQuantity($total_sets);           
    $this->set(compact('sets','onesetcost','visitor_key','shippmentCost'));
}

因此,问题是在进入"付款"页面后,"sets"变量不包含最近添加的新数据。

为了找到问题,我将记录的创建时间(我在集合表中有一个"创建"字段)与加载支付页面的时间进行了比较。(请参见注释的命令)。

这些记录似乎是在付款页面显示后30秒左右添加的。这怎么可能?

我的意思是,重定向是在记录被添加到DB之后处理的。我以为是缓存,但我仍然使用默认的文件引擎缓存,它似乎不缓存记录,只缓存数据库方案。

我一直在寻找任何相关的问题,但最终一无所获,我调试并尝试了几乎所有的想法,但仍然没有找到问题的根源。

谢谢。

window.location = paymentPage;代码封装在成功处理程序中。

例如:

$.post('/sets/handle', {k:'',setid:setid,setData:slotsData}, function(data){
    // Do nothing in here
}).done(function( msg ) {
    window.location = paymentPage;
});

发生的情况是,在请求发生的同时,用户被重定向到新页面。如果你等到请求完成,那么你应该没事。

关于jquery ajax请求的更多信息,请点击此处:http://api.jquery.com/jquery.ajax/