Php memcache sharing

Source: Internet
Author: User

This article mainly introduces how to use the php memcache class (memcache Queue). For more information, see

The memcacheQueue. class. php code is as follows: <? Php/*** PHP memcache queue class * @ author LKK/lianq.net * @ 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. thanks to FoxHunter for his valuable comments. * @ example: * $ obj = new memcacheQueue ('duilie'); * $ obj-> add ('1asdf '); * $ obj-> getQueueLength (); * $ obj-> read (10); * $ obj-> get (8); */class memcacheQueue {public static $ client; // memcache client connects to public $ access; // whether the queue can be updated private $ expire; // expired Time, second, 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 const MAXNUM = 20000; // The maximum number of queues, it is recommended that the upper limit be 10 K const HEAD_KEY = '_ lkqueuehead _'; // The first queue kye const TAIL_KEY = '_ lkqueuetail _'; // key const VALU_KEY = '_ lkqueuevalu _' At the end of the queue; // queue value key const LOCK_KEY = '_ lkqueuelock _'; // queue Lock key/*** constructor * @ Param string $ queueName queue name * @ param int $ expire expiration time * @ param array $ config memcache configuration ** @ return <type> */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 (se Lf: $ 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-> currentHea D, false, $ this-> expire);}/*** 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 (B Ool) ($ 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 Duration is lost because of 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 arra Y */public function 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 (from the end of the queue to the first of the queue) $ tmpMax = $ this-> currentTa Il; $ 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 (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 ;}$ 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 c Lear () {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.