Memcache消息队列原理与PHP实现案例
Memcache消息队列的核心机制在于对Key的设计,通常采用特定前缀配合连续递增的数字来标识序列化的消息或日志条目。随后,借助定时任务脚本将这些暂存的数据持久化写入文件或数据库系统中。
在PHP开发中,消息队列常用于解决耗时操作带来的阻塞问题,例如批量发送邮件时的高延迟场景。市面上存在多种轻量级队列服务器以简化实现:Starling是一款支持Memcache协议的轻量级持久化服务器(https://github.com/starling/starling);Beanstalkd则以高效著称,支持持久化且每秒可处理约3000个队列请求(http://kr.github.com/beanstalkd/)。此外,开发者也可直接利用PHP的Memcache或Memcached扩展自行构建消息队列逻辑。
复制代码 代码如下:
基于PHP共享内存机制实现的消息队列示例:
复制代码 代码如下:
对于规模庞大的消息队列,频繁执行大规模数据的序列化与反序列化操作会消耗大量系统资源。为解决这一性能痛点,下面展示一种PHP实现的消息队列方案。该方案仅在队列尾部进行数据插入操作,无需读取或修改整个队列结构,从而显著降低开销。需要注意的是,此实现并非线程安全,旨在尽可能减少冲突概率。若消息频率较低(如数秒才产生一条),该方案仍具可用性。若需实现线程安全,建议引入文件锁机制进行操作保护。具体代码如下:
复制代码 代码如下: class Memcache_Queue { private $memcache; private $name; private $prefix; function __construct($maxSize, $name, $memcache, $prefix = "__memcache_queue__") { if ($memcache == null) { throw new Exception("memcache object is null, new the object first."); } $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; } function isEmpty() { return $this->size == 0; } function isFull() { return $this->size == $this->maxSize; } function enQueue($data) { if ($this->isFull()) { throw new Exception("Queue is Full"); } $this->increment("size"); $this->set($this->real, $data); $this->set("real", ($this->real + 1) % $this->maxSize); return $this; } function deQueue() { if ($this->isEmpty()) { throw new Exception("Queue is Empty"); } $this->decrement("size"); $this->delete($this->front); $this->set("front", ($this->front + 1) % $this->maxSize); return $this; } function getTop() { return $this->get($this->front); } function getAll() { return $this->getPage(); } function getPage($offset = 0, $limit = 0) { if ($this->isEmpty() || $this->size < $offset) { return null; } $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; } } return array_values($this->memcache->get($keys)); } function makeEmpty() { $keys = $this->getAllKeys(); foreach ($keys as $value) { $this->delete($value); } $this->delete("real"); $this->delete("front"); $this->delete("size"); $this->delete("maxSize"); } private function getAllKeys() { if ($this->isEmpty()) { return array(); } $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; } private function add($pos, $data) { $this->memcache->add($this->getKeyByPos($pos), $data); return $this; } private function increment($pos) { return $this->memcache->increment($this->getKeyByPos($pos)); } private function decrement($pos) { return $this->memcache->decrement($this->getKeyByPos($pos)); } private function set($pos, $data) { $this->memcache->set($this->getKeyByPos($pos), $data); return $this; } private function get($pos) { return $this->memcache->get($this->getKeyByPos($pos)); } private function delete($pos) { return $this->memcache->delete($this->getKeyByPos($pos)); } private function getKeyByPos($pos) { return $this->prefix . $this->name . $pos; } }