|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
看看西,人家这个编论坛,那个CMS,还有那啥CRM,我啥时候写一个呢?如今memcache在服务器缓存使用对照普遍,上面我来先容memcache完成动静行列守候的一个例子,有必要懂得的伴侣可参考。memche动静行列的道理就是在key上做文章,用以做一个一连的数字加上前缀纪录序列化今后动静大概日记。然后经由过程准时程序将内容落地到文件大概数据库。
php完成动静行列的用途好比在做发送邮件时发送大批邮件很费工夫的成绩,那末能够接纳行列。
便利完成行列的轻量级行列服务器是:
starling撑持memcache协定的轻量级耐久化服务器
https://github.com/starling/starling
Beanstalkd轻量、高效,撑持耐久化,每秒可处置3000摆布的行列
http://kr.github.com/beanstalkd/
php中也能够利用memcache/memcached来完成动静行列。- [/code]
- 基于PHP共享内存完成的动静行列:
- [code]
复制代码
关于一个很年夜的动静行列,频仍举行举行年夜数据库的序列化和反序列化,有太泯灭。上面是我用PHP完成的一个动静行列,只必要在尾部拔出一个数据,就操纵尾部,不必操纵全部动静行列举行读取,与操纵。可是,这个动静行列不是线程平安的,我只是只管的制止了抵触的大概性。假如动静不长短常的麋集,好比几秒钟才一个,仍是能够思索如许利用的。
假如你要完成线程平安的,一个倡议是经由过程文件举行锁定,然落后行操纵。上面是代码:
代码以下:- classMemcache_Queue{private$memcache;private$name;private$prefix;function__construct($maxSize,$name,$memcache,$prefix="__memcache_queue__"){if($memcache==null){thrownewException("memcacheobjectisnull,newtheobjectfirst.");}$this->memcache=$memcache;$this->name=$name;$this->prefix=$prefix;$this->maxSize=$maxSize;$this->front=0;$this->real=0;$this->size=0;}function__get($name){return$this->get($name);}function__set($name,$value){$this->add($name,$value);return$this;}functionisEmpty(){return$this->size==0;}functionisFull(){return$this->size==$this->maxSize;}functionenQueue($data){if($this->isFull()){thrownewException("QueueisFull");}$this->increment("size");$this->set($this->real,$data);$this->set("real",($this->real+1)%$this->maxSize);return$this;}functiondeQueue(){if($this->isEmpty()){thrownewException("QueueisEmpty");}$this->decrement("size");$this->delete($this->front);$this->set("front",($this->front+1)%$this->maxSize);return$this;}functiongetTop(){return$this->get($this->front);}functiongetAll(){return$this->getPage();}functiongetPage($offset=0,$limit=0){if($this->isEmpty()$this->size<$offset){returnnull;}$keys[]=$this->getKeyByPos(($this->front+$offset)%$this->maxSize);$num=1;for($pos=($this->front+$offset+1)%$this->maxSize;$pos!=$this->real;$pos=($pos+1)%$this->maxSize){$keys[]=$this->getKeyByPos($pos);$num++;if($limit>0&&$limit==$num){break;}}returnarray_values($this->memcache->get($keys));}functionmakeEmpty(){$keys=$this->getAllKeys();foreach($keysas$value){$this->delete($value);}$this->delete("real");$this->delete("front");$this->delete("size");$this->delete("maxSize");}privatefunctiongetAllKeys(){if($this->isEmpty()){returnarray();}$keys[]=$this->getKeyByPos($this->front);for($pos=($this->front+1)%$this->maxSize;$pos!=$this->real;$pos=($pos+1)%$this->maxSize){$keys[]=$this->getKeyByPos($pos);}return$keys;}privatefunctionadd($pos,$data){$this->memcache->add($this->getKeyByPos($pos),$data);return$this;}privatefunctionincrement($pos){return$this->memcache->increment($this->getKeyByPos($pos));}privatefunctiondecrement($pos){$this->memcache->decrement($this->getKeyByPos($pos));}privatefunctionset($pos,$data){$this->memcache->set($this->getKeyByPos($pos),$data);return$this;}privatefunctionget($pos){return$this->memcache->get($this->getKeyByPos($pos));}privatefunctiondelete($pos){return$this->memcache->delete($this->getKeyByPos($pos));}privatefunctiongetKeyByPos($pos){return$this->prefix.$this->name.$pos;}}
复制代码
另外要叮嘱各位的是,抵御诱惑,ASP/PHP/JSP/.NET的对比也许会让你无所适从,你也许学了一半PHP,又开始打C#的主意,或者有人说JAVA很强,这个时候的你绝对不能动摇,哪怕你真想学。 |
|