PHP memcache class sharing _php Tutorial

Source: Internet
Author: User
This article mainly introduces the use of PHP Memcache class (Memcache queue), the need for friends can refer to the following

The memcacheQueue.class.php 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 update private $expire;//Expiration time, seconds, 1~25920 00, that is, within 30 days of private $sleepTime; Wait for unlock time, microsecond private $queueName; Queue name, unique value private $retryNum; Number of retries, = 10 * Theory concurrent number public $currentHead; The current team first value public $currentTail; Current team Tail value const MAXNUM = 20000; Maximum number of queues, recommended upper 10K const Head_key = ' _lkkqueuehead_ '; Queue First Kye 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 expiry time * @param array $config memcache configuration * * @retur N */Public Function __construct ($queueName = ", $expire =0, $config =") {if (empty ($config)) {self:: $client = Memcache_pcon Nect (' 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 to continue execution of Set_time_limit (0);//Cancel Script execution delay limit $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 set queue end/END value */Private Function _initsetheadntail () {////Current Queue header value $this->currenthead = Memcache_get (self:: $client , $this->head_key); if ($this->currenthead = = = False) $this->currenthead = 0; The value of the current queue tail $this->currenttail = Memcache_get (self:: $client, $this->tail_key); if ($this->currenttail = = = False) $this->currenttail = 0; }/** * When removing an element, change the value of the first line of the queue * @param int $step Step value */Private Function _changehead ($step =1) {$this->currenthead + = $step; m Emcache_set (self:: $client, $this->head_key, $this->currenthead,false, $this->expire); }/** * When adding elements, change the value of the end of the queue * @param int $step Step value * @param bool $reverse is reversed * @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); }/** * Queue is empty * @return BOOL */Private function_isempty () {return (bool) ($this->currenthead = = = $this->currenttail),/** * queue is full * @return bool */Private FUNCT Ion _isfull () {$len = $this->currenttail-$this->currenthead; return (bool) ($len = = = Self::maxnum),/** * Queue lock */ 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) {//try to wait n times to return false; Break }} $this->_initsetheadntail (); return $this->access = true; } return $this->access; }/** * Queue unlocked */Private Function _unlock () {Memcache_delete (self:: $client, $this->lock_key, 0); $this->access = Fals E /** * Gets the length of the current queue * This length is the theoretical length, some elements are lost due to expiration, the true length <= the length * @return int */Public Function getqueuelength () {$this->_init Setheadntail (); Return Intval ($this->currenttail-$this->currenthead); /** * Add queue data * @param void $data The data to be added * @return BOOL */Public Function Add ($data) {if (!$this->_getlock ()) return false; if ($this->_isfull ()) {$this->_unlock (); return false;} $value _key = $this->queuename. Self::valu_key. Strval ($this->currenttail + 1); $result = Memcache_set (self:: $client, $value _key, $data, memcache_compressed, $this->expire); if ($result) {$this->_changetail ();} $this->_unlock (); return $result; /** * Read queue data * @param int $length The length to read (reverse read using negative numbers) * @return Array */Public function read ($length =0) {if (!is_numeric ($l Ength)) return false; $this->_initsetheadntail (); if ($this->_isempty ()) {return false;} if (Empty ($length)) $length = self::maxnum;//default All $KEYARR = Array (); if ($length >0) {//forward read (from queue header to end of queue) $tmpMin = $this->currenthead; $tmpMax = $tmpMin + $length; for ($i = $tmpMin; $i <=$ Tmpmax; $i + +) {$KEYARR [] = $this->queuename. Self::valu_key. $i;}} else{//reverse read (from queue tail to top of queue) $tmpMax = $this->currenttail; $tmpMin = $tmpMax + $length; for ($i = $tmpMax; $i > $tmpMin; $i-) {$KEYARR [] = $this->queuename. Self:: Valu_key. $i; }} $result = @memcache_get (self:: $client, $KEYARR); return $result; /** * Take out queue data * @param int $length The length to be fetched (reverse read using negative numbers) * @return Array */Public function get ($length =0) {if (!is_numeric ($le Ngth)) return false; if (! $this->_getlock ()) return false; if ($this->_isempty ()) {$this->_unlock (); return false;} if (Empty ($length)) $length = self::maxnum;//Default All $length = Intval ($length); $KEYARR = Array (); if ($length >0) {//forward read (from queue header to end of queue) $tmpMin = $this->currenthead; $tmpMax = $tmpMin + $length; for ($i = $tmpMin; $i <=$ Tmpmax; $i + +) {$KEYARR [] = $this->queuename. Self::valu_key $i;} $this->_changehead ($length); }else{//reverse read (from queue tail to top of queue) $tmpMax = $this->currenttail; $tmpMin = $tmpMax + $length; for ($i = $tmpMax; $i > $tmpMin; $i-- {$KEYARR [] = $this->queuename. Self::valu_key. $i;} $this->_changetail (ABS ($length), true); } $result = @memcache_get (self:: $client, $KEYARR); foreach ($keyArr as $v) {//Remove @memcache_delete after removal (self:: $client, $v, 0); } $this->_unlock (); return $result; }/** * Empty queue */Public function Clear () {if (! $this->_getlock ()) return false; if ($this->_isempty ()) {$this->_unlo CK (); return false; } $tmpMin = $this->currenthead--; $tmpMax = $this->currenttail++; for ($i = $tmpMin; $i <= $tmpMax; $i + +) {$tmpKey = $this->queuename. Self::valu_key. $i; @memcache_delete (self:: $clie NT, $tmpKey, 0); } $this->currenttail = $this->currenthead = 0; Memcache_set (self:: $client, $this->head_key, $this->currenthead,false, $this->expire); Memcache_set (self:: $client, $this->tail_key, $this->currenttail,false, $this->expire); $this->_unlock (); }/* Clears all memcache cache data */Public Function Memflush () {Memcache_flush (self:: $client);}} End Class

http://www.bkjia.com/PHPjc/746591.html www.bkjia.com true http://www.bkjia.com/PHPjc/746591.html techarticle This article mainly introduces the use of PHP Memcache class (Memcache queue), the need for friends can refer to the following memcacheQueue.class.php code:? php/*** PHP Memcache Queue class * @aut ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.