在我的普通民意调查Laravel聊天应用中,我会将用户发送的新消息保存到文件缓存中,并将密钥作为字符串,从date(current_time)
函数中获取其值信息的主体。
然后,当我想获取这些消息时,我将使用最后一个轮询值$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 :(得分:0)
为什么不通过基于时间戳或密钥创建缓存文件来尝试这样的事情:
有关详情及建议,请参阅:http://evertpot.com/107/
//这是用函数存储信息的函数 store($ key,$ data,$ 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);
}
//用于查找某个私钥的文件名的常规函数 function getFileName($ key){
return '/tmp/s_cache' . md5($key);
}
//获取数据的函数在失败函数时返回false fetch($ 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]; }
}