This article mainly introduces how to use PHP’s memcache class (memcache queue). Friends in need can refer to it
memcacheQueue.class.php
The code is as follows:
add('1asdf');
* $obj->getQueueLength();
* $obj->read(10);
* $obj->get(8);
*/
class memcacheQueue{
public static $client; //memcache client connection
public $access; //Whether the queue can be updated
private $expire; //Expiration time, seconds, 1~2592000, that is, within 30 days
private $sleepTime; //Waiting time for unlocking, microseconds
private $queueName; //Queue name, unique value
private $retryNum; //Number of retries, = 10 * theoretical concurrency number
public $currentHead; //Current team head value
public $currentTail; //Current tail value
const MAXNUM = 20000; //Maximum number of queues, the recommended upper limit is 10K
const HEAD_KEY = '_lkkQueueHead_'; //The first key of the queue
const TAIL_KEY = '_lkkQueueTail_'; //queue tail key
const VALU_KEY = '_lkkQueueValu_'; //queue value key
const LOCK_KEY = '_lkkQueueLock_'; //Queue lock key
/*** Constructor
* @param string $queueName Queue name
* @param int $expire expiration time
* @param array $config memcache configuration
*
* @return
*/
public function __construct($queueName ='',$expire=0,$config =''){
if(empty($config)){
Self::$client = memcache_pconnect('127.0.0.1',11211);
}elseif(is_array($config)){//array('host'=>'127.0.0.1','port'=>'11211')
self::$client = memcache_pconnect($config['host'],$config['port']);
}elseif(is_string($config)){//"127.0.0.1:11211"
$tmp = explode(':',$config);
$conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1';
$conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211';
Self::$client = memcache_pconnect($conf['host'],$conf['port']);
}
if(!self::$client) return false;
ignore_user_abort(true);//When the client disconnects, allow execution to continue
set_time_limit(0);//Cancel the upper limit of script execution delay
$this->access = false;
$this->sleepTime = 1000;
$expire = empty($expire) ? 3600 : intval($expire)+1;
$this->expire = $expire;
$this->queueName = $queueName;
$this->retryNum = 1000;
$this->head_key = $this->queueName . self::HEAD_KEY;
$this->tail_key = $this->queueName . self::TAIL_KEY;
$this->lock_key = $this->queueName . self::LOCK_KEY;
$this->_initSetHeadNTail();
}
/*** Initialize and set the head and tail values of the queue
*/
private function _initSetHeadNTail(){
//The value at the head of the current queue
$this->currentHead = memcache_get(self::$client, $this->head_key);
if($this->currentHead === false) $this->currentHead =0;
//The value at the end of the current queue
$this->currentTail = memcache_get(self::$client, $this->tail_key);
if($this->currentTail === false) $this->currentTail =0;
}
/*** When taking out an element, change the value at the head of the queue
* @param int $step step value
*/
private function _changeHead($step=1){
$this->currentHead += $step;
memcache_set(self::$client, $this->head_key,$this->currentHead,false,$this->expire);
}
/*** When adding an element, change the value at the end of the queue
* @param int $step step value
* @param bool $reverse Whether to reverse
* @return null
*/
private function _changeTail($step=1, $reverse =false){
if(!$reverse){
$this->currentTail += $step;
}else{
$this->currentTail -= $step;
}
memcache_set(self::$client, $this->tail_key,$this->currentTail,false,$this->expire);
}
/*** Whether the queue is empty
* @return bool
*/
private function _isEmpty(){
return (bool)($this->currentHead === $this->currentTail);
}
/*** Is the queue full?
* @return bool
*/
private function _isFull(){
$len = $this->currentTail - $this->currentHead;
return (bool)($len === self::MAXNUM);
}
/*** Queue locking
*/
private function _getLock(){
if($this->access === false){
while(!memcache_add(self::$client, $this->lock_key, 1, false, $this->expire) ){
usleep($this->sleepTime);
@$i++;
if($i > $this->retryNum){//尝试等待N次
return false;
break;
}
}
$this->_initSetHeadNTail();
return $this->access = true;
}
return $this->access;
}
/*** Queue unlocking
*/
private function _unLock(){
memcache_delete(self::$client, $this->lock_key, 0);
$this->access = false;
}
/*** Get the length of the current queue
* This length is the theoretical length, some elements are lost due to expiration, the real length
http://www.bkjia.com/PHPjc/746591.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/746591.htmlTechArticle这篇文章主要介绍了php的memcache类的使用方法(memcache队列),需要的朋友可以参考下 memcacheQueue.class.php代码如下:?php/*** PHP memcache 队列类* @aut...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn