Use complete spike rush redis watch function (rpm)

redis use watch spike rush to complete the function:

Redis completed using two key functions rush spike, mywatchkey for storing the number of users buy and store mywatchlist buy list.

Its advantages are as follows:

1. First choose to buy fast memory database.

2. Fast concurrent nature no not a problem.

3. Use the pessimistic locking, quickly increasing system resources.

4. stronger than the queue, the queue will instantly bursting your memory database resources.

5. Use optimistic locking, achieve comprehensive needs.

 

I think the following code is certainly what you want.

 
  1. <?php  
  2. header("content-type:text/html;charset=utf-8");  
  3. $redis = new redis();  
  4. $result = $redis->connect('10.10.10.119', 6379);  
  5. $mywatchkey = $redis->get("mywatchkey");  
  6. rob_total = 100 $;    // number of panic buying  
  7. if($mywatchkey<$rob_total){  
  8.     $redis->watch("mywatchkey");  
  9.     $redis->multi();  
  10.       
  11.     // set the delay, convenient test results.  
  12.     sleep(5);  
  13.     // insert snapped data  
  14.     $redis->hSet("mywatchlist","user_id_".mt_rand(1, 9999),time());  
  15.     $redis->set("mywatchkey",$mywatchkey+1);  
  16.     $rob_result = $redis->exec();  
  17.     if($rob_result){  
  18.         $mywatchlist = $redis->hGetAll("mywatchlist");  
  19.         echo  "buy success <br/>!";  
  20.         echo  "remaining number:" (. $ rob_total- $-mywatchkey. 1). "a";  
  21.         echo  "User List: <pre>";  
  22.         var_dump($mywatchlist);  
  23.     }else{  
  24.         echo  "I'm feeling good, then buy!"; Exit;  
  25.     }  
  26. }  
  27. ?>  

Guess you like

Origin www.cnblogs.com/WebLinuxStudy/p/11425778.html