search
HomeBackend DevelopmentPHP Tutorialmemcache builds a simple memory message queue_PHP tutorial

memcache builds a simple memory message queue_PHP tutorial

Jul 13, 2016 am 10:46 AM
memcacheintroduceuseMemoryclassmatearticleConstructinformationofSimplequeue

This article will introduce to you how to use memcache to build a simple memory message queue. I will introduce it to you with a relatively good example. I hope this method will be helpful to everyone.

The memcache function is too simple. It can only set get and delete. It can only save key-value data, but cannot save lists. Of course, you can also serialize a list and store it in memcache, but there will be concurrency problems. Every time you save data (queue insertion or dequeue), the data must be locked, which is difficult to guarantee in high concurrency situations. Data consistency!
But memcache has an increment operation, which adds 1 to the value corresponding to a certain key (actually an addition operation, plus 1 by default). This operation is atomic, so we can use this to maintain an auto-incrementing ID. Ensure the uniqueness of data. Add two pointers to maintain the starting key value, and you build a simple single-phase queue! !         

memcache queue

Upload code: 

The code is as follows Copy code
 代码如下 复制代码
/**
 * memcache构建的简单内存队列
 *
 * @author: jeffjing
 */
class memList {
 private $memcache; // memcache类
 
 private $queKeyPrefix; //数据键前缀
 private $startKey; //开始指针键
 private $startKey; //结束指针键
 
 public function __construct($key){
  $this->queKeyPrefix = "MEMQUE_{$key}_";
  $this->startKey = "MEMQUE_SK_{$key}";
  $this->endKey = "MEMQUE_EK_{$key}"; 
 }
 
 /**
  * 获取列表
  *  先拿到开始结束指针, 然后去拿数据
  *
  * @return array
  */
 public function getList(){
  $startP = $this->memcache->get($this->startKey);
  $endP = $this->memcache->get($this->endKey);  
  empty($startP) && $startP = 0;
  empty($endP) && $endP = 0;
 
  $arr = array();
  for($i = $startP ; $i    $key = $this->queKeyPrefix . $i;
   $arr[] = $this->memcache->get($key);
  }
  return $arr;
 }
 
 /**
  * 插入队列
  *   结束指针后移,拿到一个自增的id
  *   再把值存到指针指定的位置
  *  
  *   @return void
  */
 public function in($value){
  $index = $this->memcache->increment($this->endKey);
  $key = $this->queKeyPrefix . $index;
  $this->memcache->set($key, $value);
 }
 
 /**
  * 出队
  *    很简单, 把开始值取出后开始指针后移
  *
  * @return mixed
  */
 public function out(){
  $result = $this->memcache->get($this->startKey);
  $this->memcache->increment($this->startKey);
  return $result;
 }
 
}
/** * Simple memory queue built by memcache * * @author: jeffjing ​*/ class memList { private $memcache; // memcache class private $queKeyPrefix; //Data key prefix private $startKey; //Start pointer key private $startKey; //End pointer key public function __construct($key){ $this->queKeyPrefix = "MEMQUE_{$key}_"; $this->startKey = "MEMQUE_SK_{$key}"; $this->endKey = "MEMQUE_EK_{$key}"; } /** * Get list * Get the start and end pointers first, then get the data * * @return array ​*/ public function getList(){ $startP = $this->memcache->get($this->startKey); $endP = $this->memcache->get($this->endKey); empty($startP) && $startP = 0; empty($endP) && $endP = 0; $arr = array(); for($i = $startP ; $i $key = $this->queKeyPrefix . $i; $arr[] = $this->memcache->get($key); } return $arr; } /** * Insert into queue * Move the end pointer back and get an auto-incremented id * Then store the value to the location specified by the pointer * * @return void ​*/ public function in($value){ $index = $this->memcache->increment($this->endKey); $key = $this->queKeyPrefix . $index; $this->memcache->set($key, $value); } /** * Departure * It’s very simple, take out the start value and move the start pointer backward * * @return mixed ​*/ public function out(){ $result = $this->memcache->get($this->startKey); $this->memcache->increment($this->startKey); return $result; } }

Some things about memcached


Memory storage method (slab allocator)

The data storage method of memcached is slab allocator, which is data sharding. When the service is started, the memory is divided into chunks of different sizes. When data comes, it is stored in a chunk of appropriate size
The previous version allocates memory directly, causing problems such as memory fragmentation and random searches. . .


Data expiration deletion mechanism


Memcached will not delete the data after it expires, but it cannot access expired data, and the space occupied by expired data will be reused
Memcached uses lazy expiration. It does not actively scan whether a data item has expired, but determines whether it has expired when the data is obtained.
The deletion algorithm is LRU (Least Recently Used), which gives priority to deleting data that has been used less recently


Memcached’s distributed mechanism


Although memcached is a distributed cache, memcached itself does not implement any distributed mechanism. The distributed function is mainly implemented by the client.
The program adds multiple memcahced services to the client (memcache extension) through addserver. Before accessing data, the client will first obtain the node where the data is stored through the hash algorithm, and then access the data. When one of the memcached servers hangs up, Or if you add a new memcached server, the node where the data is stored based on the hash algorithm will change, and the new server will be used to access the data.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632905.htmlTechArticleThis article will introduce to you how to use memcache to build a simple memory message queue, using a relatively good example. Everyone introduces it. I hope this method will be helpful to everyone. memcache function...
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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.