ホームページ  >  記事  >  バックエンド開発  >  PHP でのメッセージ キューの実装 Memcache_PHP チュートリアル

PHP でのメッセージ キューの実装 Memcache_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:43:02920ブラウズ

メッセージ キューが大きい場合、大規模なデータベースを頻繁にシリアル化および逆シリアル化すると、時間がかかりすぎます。以下は、PHP を使用して実装したメッセージ キューです。末尾にデータを挿入し、末尾を操作するだけで済みます。読み取りや操作のためにメッセージ キュー全体を操作する必要はありません。ただし、このメッセージ キューはスレッドセーフではありません。競合の可能性を回避しようとしているだけです。メッセージの密度がそれほど高くない場合 (たとえば、数秒に 1 つのメッセージだけである場合)、この方法での使用を検討できます。
スレッドの安全性を実現したい場合、ファイルをロックしてから操作することをお勧めします。コードは次のとおりです。

コードをコピーします。 コードは次のとおりです。
class Memcache_Queue
{
private $memcache;
function __construct($maxSize, $name; , $memcache, $prefix = "__memcache_queue__")
{
if ($memcache == null) {
throw new Exception("memcache オブジェクトが null、最初にオブジェクトを新規作成します。");
$this-> memcache = $memcache;
$this->名前 = $name;
$this->maxSize = $maxSize; this->get($name);
$this->size = 0;
関数 __get($name)
}
関数 __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()) {
新規をスローException("キューがいっぱいです");
$this->increment("size");
$this->set($this->real, $data); ("real" , ($this->real + 1) % $this->maxSize);
return $this;
関数 deQueue()
{
if ($this->isEmpty()) {
throw new Exception("Queue is Empty")
}
$this->decrement("size");
$this-> set("フロント ", ($this->front + 1) % $this->maxSize);
return $this;
function getTop()
{
return $this->get($this ->フロント );
}
関数 getAll()
{
return $this->getPage();
}
関数 getPage($offset = 0, $limit = 0)
{
if ($this- >isEmpty( ) || $this->size < $offset) {
null を返す
}
$keys[] = $this->getKeyByPos(($this->front + $offset) % $this->maxSize);
$num = 1;
for ($pos = ($this->front + $offset + 1) % $this->maxSize; $pos != $this->実数; $pos = ($pos + 1) % $this->maxSize)
$keys[] = $this->getKeyByPos($pos);
if ($limit > 0) && $limit = = $num) {
break;
}
}
return array_values($this->memcache->get($keys))
}
function makeEmpty()
{
$keys = $ this->getAllKeys();
foreach ($keys を $value) {
$this->delete("real"); ;delete(" フロント");
$this->delete("maxSize");
}
プライベート関数 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;
}
プライベート関数 add($pos, $data)
{
$this->memcache->add($this->getKeyByPos($) pos), $ data);
return $this;
}
private 関数 increment($pos)
{
return $this->memcache->increment($this->getKeyByPos($pos)); }
プライベート関数 decrement($pos)
{
$this->memcache->decrement($this->getKeyByPos($pos))
}
プライベート関数 set($pos, $data)
{
$this->memcache->set($this->getKeyByPos($pos), $data);
return $this;
プライベート関数 get($pos)
{
return $this-> ;memcache-> ;get($this->getKeyByPos($pos));
}
プライベート関数 delete($pos)
{
return $this->memcache->delete($this->getKeyByPos) ($pos) );
プライベート関数 getKeyByPos($pos)
$this->prefix . $pos;

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/320816.html技術記事メッセージ キューが大きい場合、大規模なデータベースを頻繁にシリアル化および逆シリアル化すると、時間がかかりすぎます。以下は PHP を使用して実装したメッセージ キューです。最後に...
を挿入するだけです。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。