PHPmemcache queue class

Source: Internet
Author: User
PHPmemcache queue class none? Php *** PHPmemcache queue class * @ authorLKKlianq.net * @ createon19: 222014324 * @ version0.3 * @ modify Description: * 1. I gave up the previous AB plane rotation idea, used arrays like the structure, and rewritten this class. * 2. the queue is first-in-first-out by default, but the reverse reading function is added. * 3.

PHPmemcache queue class none? Php/*** PHP memcache queue class * @ author LKK/lianq.net * @ create on * @ version 0.3 * @ modify Description: * 1. I gave up the previous AB plane rotation idea, used arrays like the structure, and rewritten this class. * 2. the queue is first-in-first-out by default, but the reverse reading function is added. * 3.

PHP memcache queue <无>
  Add ('1asdf '); * $ obj-> getQueueLength (); * $ obj-> read (10); * $ obj-> get (8 ); */class memcacheQueue {public static $ client; // The memcache client connects to public $ access; // whether the queue can update private $ expire; // expiration time, seconds, 1 ~ 2592000, that is, private $ sleepTime within 30 days; // waiting for unlock time, microsecond private $ queueName; // queue name, unique value private $ retryNum; // Number of retries, = 10 * theoretical concurrency public $ currentHead; // the current team's first value public $ currentTail; // the current team's tail value constMAXNUM = 20000; // The maximum number of queues, it is recommended that the upper limit be 10KconstHEAD_KEY = '_ lkqueuehead _'; // The first queue is kyeconstTAIL_KEY = '_ prop _'; // the end of the queue is keyconstVALU_KEY = '_ lkqueuevalu _'; // queue value keyconstLOCK_KEY = '_ lkqueuelock _'; // 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 ('2017. 0.0.1 ', 11211);} elseif (is_array ($ config) {// array ('host' => '2017. 0.0.1 ', 'Port' => '000000') 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]: '192. 0.0.1 '; $ conf ['Port'] = isset ($ tmp [1])? $ Tmp [1]: '000000'; self: $ client = memcache_pconnect ($ conf ['host'], $ conf ['Port']);} if (! Self: $ client) return false; ignore_user_abort (true); // when the customer disconnects, set_time_limit (0) can be continued ); // cancel the maximum latency of script execution $ 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 first and last values of the queue */private function _ initSetHeadNTail () {// the first value 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 an element is retrieved, change the first value of the queue * @ param int $ step value */private function _ changeHead ($ step = 1) {$ this-> currentHead + = $ step; memcache_set (self:: $ client, $ this-> head_key, $ this-> currentHead, false, $ this-> expi Re);}/*** when an element is added, change the value at the end of the queue * @ param int $ step value * @ param bool $ whether 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 );} /*** whether the queue is empty * @ return bool */private function _ isEmpty () {return (bool) ($ this-> currentHead ===$ this-> currentTail);}/*** whether the queue is full * @ return bool */private function _ 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 for N times return false; break ;}$ this-> _ initSetHeadNTail (); return $ this-> access = true;} return $ this-> access;}/*** unLock queue */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 actual length <= this length * @ Return int */public function getQueueLength () {$ this-> _ initSetHeadNTail (); 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 be read (negative number used for reverse READING) * @ return array */public f Unction read ($ length = 0) {if (! Is_numeric ($ length) return false; $ this-> _ initSetHeadNTail (); if ($ this-> _ isEmpty () {return false ;} if (empty ($ length) $ length = self: MAXNUM; // All $ keyArr = array () by default; if ($ length> 0) {// forward reading (from the beginning to the end of the queue) $ tmpMin = $ this-> currentHead; $ tmpMax = $ tmpMin + $ length; for ($ I = $ tmpMin; $ I <= $ tmpMax; $ I ++) {$ keyArr [] = $ this-> queueName. self: VALU_KEY. $ I ;}} else {// reverse read (beginning from the end of the queue to the beginning of the 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 ;} /*** retrieve queue data ** @ param int $ length the length to be retrieved (negative number used for reverse READING) * @ return array */public function get ($ length = 0) {if (! Is_numeric ($ length) return false; if (! $ This-> _ getLock () return false; if ($ this-> _ isEmpty () {$ this-> _ unLock (); return false ;} if (empty ($ length) $ length = self: MAXNUM; // All $ length = intval ($ length) by default; $ keyArr = array (); if ($ length> 0) {// forward read (from the beginning to the end of the 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 the end of the queue to the queue) First) $ 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) {// Delete @ memcache_delete (self: $ client, $ v, 0) after removal;} $ this-> _ unLock (); return $ result;}/*** clear queue */public function clear () {if (! $ This-> _ getLock () return false; if ($ this-> _ isEmpty () {$ this-> _ unLock (); return false ;} $ tmpMin = $ this-> currentHead --; $ tmpMax = $ this-> currentTail ++; for ($ I = $ tmpMin; $ I <= $ tmpMax; $ I ++) {$ tmpKey = $ this-> queueName. self: VALU_KEY. $ I; @ memcache_delete (self: $ client, $ 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 ();}/** clear all memcache cache data */public function memFlush () {memcache_flush (self: $ client) ;}// end class
  

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.