linux下可以轻松的利用PHP扩展做一个更完善的,独立服务的队列。但是WIN下相对就麻烦了。这个类只基于PHP核心函数。
原理:利用数组实现队列,讲数组序列化存入文档以实现数据共享。其中存储文件读写时加锁,防止同时操作造成脏读。
1.[代码][PHP]代码
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: xavier007 <499873958@qq.com> |
// +----------------------------------------------------------------------+
//
// $Deque=new Deque(__DIR__.'/cache.txt');
// $Deque->addFirst();
// Deque:实现了PHP队列。可以在多进程之间共享队列信息。
//
//
class Deque{
private $list_array;
private $filename;
public function __construct($filename="cache.txt") {
$this->list_array=array();
$this->filename=$filename;
}
private function _write($data=array()){
$str=serialize($data);
$fp = fopen($this->filename, 'w+');
if(flock($fp,LOCK_EX)) {
$arr['name'] = 'foo2';
ftruncate($fp, 0);
fwrite($fp,$str);
fflush($fp);
flock($fp, LOCK_UN);
}
fclose($fp);
}
private function _read(){
try
{
$file = $this->filename;
if (file_exists($file )){
$fp = fopen($file , 'r+');
$str="";
if(flock($fp , LOCK_EX)){
while (!feof($fp)) {
$str .= fread($fp, 100);
}
flock($fp , LOCK_UN);
} else{
echo "Lock file failed...\n";
}
fclose($fp);
$data=unserialize($str);
return $data;
}
return array();;
}
catch(Exception $e)
{
return array();;
}
}
private function _update(){
$arr=self::_write($this->list_array);
}
/**(尾部)入队 **/
public function addLast($value)
{
$arr=self::_read();
$this->list_array=$arr;
array_push($this->list_array,$value);
$arr=self::_update();
return $this->list_array;
}
/**(尾部)出队**/
public function removeLast()
{
$arr=self::_read();
$this->list_array=$arr;
array_pop($this->list_array);
$arr=self::_update();
return $this->list_array;
}
/**(头部)入队**/
public function addFirst($value)
{
$arr=self::_read();
$this->list_array=$arr;
array_unshift($this->list_array,$value);
$arr=self::_update();
return $this->list_array;
}
/**(头部)出队**/
public function removeFirst()
{
$arr=self::_read();
$this->list_array=$arr;
array_shift($this->list_array);
$arr=self::_update();
return $this->list_array;
}
/**清空队列**/
public function makeEmpty()
{
$arr=self::_read();
$this->list_array=$arr;
unset($this->list_array);
$this->list_array=array();
$arr=self::_update();
return $this->list_array;
}
/**获取列头**/
public function getFirst()
{
return reset($this->list_array);
}
/** 获取列尾 **/
public function getLast()
{
return end($this->list_array);
}
/** 获取长度 **/
public function getLength()
{
return count($this->list_array);
}
}