PHPキュー


キューとは、先進的な線形テーブルであり、具体的な応用では通常チェーンテーブルや配列で実現され、キューはバックエンドでのみ挿入操作を行い、フロントエンドで削除操作を行うことができる.
どのような場合にキューが使われるのか、同時リクエストでトランザクションの完全性が保証される場合にキューが使われるので、もちろん他のより良い方法を使うことを排除しないで、知っていることは真似してみません.
キューは、データベース・サーバの圧力を軽減するためにも使用できます.インスタントではないデータをキューに入れ、データベースが空いているときや一定時間間隔を置いて実行できます.たとえば、アクセスカウンタでは、アクセスが増加したSqlを即時に実行する必要はありません.キューを使用していない場合、sql文は5人がアクセスしていると仮定します.
  • update table1 set count=count+1 where id=1 
  • update table1 set count=count+1 where id=1 
  • update table1 set count=count+1 where id=1 
  • update table1 set count=count+1 where id=1 
  • update table1 set count=count+1 where id=1 
  • update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1 update table1 set count=count+1 where id=1

    キューを使用すると、次のようになります.
  • update table1 set count=count+5 where id=1 
  • update table1 set count=count+5 where id=1

    sqlリクエスト回数を減らすことで、サーバのストレスを軽減する効果があります.もちろん、アクセス量が大きくないサイトには必要ありません.
    次のキュー・クラス:
  • /**
  • *キュー
  • *
  • * @author jaclon
  • *
  • */ 
  • class Queue 
  • private $_queue = array(); 
  • protected $cache = null; 
  • protected $queuecachename; 
  •  
  • /**
  • *構築方法
  • *@param string$queuenameキュー名
  • */ 
  • function __construct($queuename) 
  •  
  • $this->cache =& Cache::instance(); 
  • $this->queuecachename = 'queue_' . $queuename; 
  •  
  • $result = $this->cache->get($this->queuecachename); 
  • if (is_array($result)) { 
  • $this->_queue = $result; 
  •  
  • /**
  • *1つのユニットをキュー末尾
  • に入れる
  • * @param mixed $value
  • */ 
  • function enQueue($value) 
  • $this->_queue[] = $value; 
  • $this->cache->set($this->queuecachename, $this->_queue); 
  •  
  • return $this; 
  •  
  • /**
  • *キューの先頭にある1つまたは複数のユニットを
  • から削除
  • * @param int $num
  • */ 
  • function sliceQueue($num = 1) 
  • if (count($this->_queue) < $num) { 
  • $num = count($this->_queue); 
  • $output = array_splice($this->_queue, 0, $num); 
  • $this->cache->set($this->queuecachename, $this->_queue); 
  •  
  • return $output; 
  •  
  • /**
  • *キューの先頭のユニットをキュー
  • から移動
  • */ 
  • function deQueue() 
  • $entry = array_shift($this->_queue); 
  • $this->cache->set($this->queuecachename, $this->_queue); 
  •  
  • return $entry; 
  •  
  • /**
  • *戻りキュー長
  • */ 
  • function size() 
  • return count($this->_queue); 
  •  
  • /**
  • *は、キュー内の最初のユニット
  • を返す.
  • */ 
  • function peek() 
  • return $this->_queue[0]; 
  •  
  • /**
  • *は、キュー内の1つまたは複数のユニット
  • を返す
  • * @param int $num
  • */ 
  • function peeks($num) 
  • if (count($this->_queue) < $num) { 
  • $num = count($this->_queue); 
  • return array_slice($this->_queue, 0, $num); 
  •  
  • /**
  • *破棄キュー
  • */ 
  • function destroy() 
  • $this->cache->remove($this->queuecachename); 

  • 変換元:http://blog.163.com/lgh_2002/blog/static/44017526201172511139202/