2017-10-15 530 views
0

我正在使用memcached在bot上存儲消息的一些數據,然後將其存儲到rethindkb表中。 我有我寫的用於存儲下面的代碼,還有一個用於檢索數據的函數。Memcached沒有正確地返回存儲的數據codeigniter 3

在第一所述準備數據的方法的第一步驟中(情況0)中,i建立一個數組,如本

`[id = 123]` 

並將其存儲在分佈式緩存 在第二步驟中的方法的(情況1)我檢索所存儲的數據,然後另一元件附加到數據,它看起來像這樣

[id = 123, lang = english] 

在getPrepData功能,我檢索該數據,以便檢索所存儲的存儲陣列的鍵的值。然而,當我檢索數據,並將其存儲在日誌中,郎鍵不和它看起來像這樣

[id = 123] 

出於調試目的,存儲郎鍵後,我拿來從memcached的數據和記錄它和我得到了期望的輸出。 使用PHP 7和codeigniter 3.

任何人都知道爲什麼會發生這種情況,我該如何解決它?

<?php 
class User extends CI_Model { 
    private $data; 
    private $prep_data; 
    public function __construct(){ 

    } 

    public function is_registered($id){ 
    require_once('application/third_party/rdb/rdb.php'); 
    $this->load->config('redb.php'); 
    $db = $this->config->item('redbdb'); 
    $con = r\connect(array('host' => 'localhost','db' => $db)); 
    $results = r\table('users')->filter(['tid' => $id])->count()->run($con); 
    if($results == 0){ 
     return false; 
    } 
    return true; 
    } 

    public function register_prep($id,$step,$store=null){ 
    $this->load->driver('cache'); 

    switch($step){ 
     case(0): 
     //Insert empty cache entry 
     $x = $this->cache->memcached->get('prep_'.$id); 
     if($x){ 
     $this->cache->memcached->delete('prep_'.$id); 
     } 
     $prep = ['tid' => $id]; 
     $this->cache->memcached->save('prep_'.$id,$prep,0); 
     break; 

     case(1): 
     //insert language 
     $prep = $this->cache->memcached->get('prep_'.$id); 
     $prep['lang'] = $store; 
     $this->cache->memcached->delete('prep_'.$id); 
     $this->cache->memcached->save('prep_'.$id,$prep,0); 
     $x = $this->cache->memcached->get('prep_'.$id); 
     log_message('error','saved data on step 1 is : '.print_r($x,true)); 
     break; 

     case(2): 
     //Insert driver/passenger 
     $prep = $this->cache->memcached->get('prep_'.$id); 
     $prep['type'] = $store; 
     $this->cache->memcached->save('prep_'.$id,$prep,0); 
     break; 

     case(3): 
     //Insert contact 
     $prep = $this->cache->memcached->get('prep_'.$id); 
     $prep['contact'] = $store; 
     $this->cache->memcached->save('prep_'.$id,$prep,0); 
     break; 

     case(4): 
     //Finish 
     $prep = $this->cache->memcached->get('prep_'.$id); 
     //Insert to database 
     require_once('application/third_party/rdb/rdb.php'); 
     $this->load->config('redb.php'); 
     $db = $this->config->item('redbdb'); 
     $con = r\connect(array('host' => 'localhost','db' => $db)); 
     r\table('users')->insert($prep)->run($con); 
     //Delete from cache 
     $this->cache->memcached->delete('prep_'.$id); 
     break; 
    } 
    } 

    public function getPrepdata($id,$var){ 
    $this->load->driver('cache'); 
    $x = $this->cache->memcached->get('prep_'.$id); 
    log_message('error','fetched prep data is : '.print_r($x,true)); 
    return $x[$var]; 
    } 

}

回答

-2

可以嘗試這個例子:

$data = array(id => 123); 
if(empty($data)){ 
    $data = array(); // Declaring array 
} 
print_r($data); 

結果: 陣列( ID => 123 )

現在補充一點:

$new_data = array(lang => english); 

array_push($data,$new_data); // Items will be add to array 

print_r($data); 

陣列( ID => 123, LANG =>英語 )

你想要得到的只是身份證號碼:

echo $data['id']; 
+0

你甚至看他的問題? – sintakonte