2014-06-25 23 views
0

在我的普通投票Laravel聊天應用程序中,我將用戶將發送的新郵件保存到文件緩存中,並將該密鑰作爲字符串存儲,並從date(current_time)函數和郵件正文中獲取其值。Laravel:如何比較/獲取緩存中的特定數據?

然後,當我想要獲取這些消息時,我將使用最後一個輪詢值$lastPolled = Session::get('lastPolled')並與緩存中的密鑰進行比較。大於$ lastPolled值的鍵會將其數據視爲新消息並添加到對話中。

最後,我將更新上次調查的會話值Session::put('lastPolled',date(Y-m-d H:i:s)

所以,我怎麼比較高速緩存中的所有鍵$ lastPolled並獲得每個鍵的值?沿線的東西:

$latestMessages = array(); 
foreach(KeysInCache as Key=>value){ 
    if($lastPolled>Key) 
     array_push($latestMessages,Key=>value); 
} 

謝謝!

P.s.獎勵積分更好的建議。哦,我不能使用memcache/redis/otherSuperCaches出於技術原因,只有文件/數據庫緩存。 :(

回答

0

爲什麼不基於時間戳或密鑰生成緩存文件嘗試一些像這樣的事情:

在進一步的細節和建議一樣:http://evertpot.com/107/

//這是您存儲功能與功能 商店($鍵,$數據,$ TTL){

// Opening the file 
$h = fopen($this->getFileName($key),'w'); 
if (!$h) throw new Exception('Could not write to cache'); 
// Serializing along with the TTL 
$data = serialize(array(time()+$ttl,$data)); 
if (fwrite($h,$data)===false) { 
    throw new Exception('Could not write to cache'); 
} 
fclose($h); 

}

信息

//一般功能找到某重點民營 的getFileName功能($鍵)文件名{

return '/tmp/s_cache' . md5($key); 

}

//獲取數據的函數返回的故障功能虛假 取( $鍵){

$filename = $this->getFileName($key); 
    if (!file_exists($filename) || !is_readable($filename)) return false; 

    $data = file_get_contents($filename); 

    $data = @unserialize($data); 
    if (!$data) { 

    // Unlinking the file when unserializing failed 
    unlink($filename); 
    return false; 

    } 

    // checking if the data was expired 
    if (time() > $data[0]) { 

    // Unlinking 
    unlink($filename); 
    return false; 

    } 
    return $data[1]; 
} 

}

相關問題