Redis uses watch to complete the second kill snapping feature:
Using two keys in Redis to complete the seconds Kill snapping feature, Mywatchkey is used to store snapped up quantities and mywatchlist user store snapping lists.
It has the following advantages:
1. First select the memory database to snap the speed extremely fast.
2. Fast concurrency is not a problem, naturally.
3. Using pessimistic locks can quickly increase system resources.
4. More than the team powers, the queue will make your memory database resources instantly bursting.
5. Use optimistic lock to achieve comprehensive requirements.
I think the following code is definitely what you want.
[PHP]View PlainCopyprint?
- <?php
- Header ("Content-type:text/html;charset=utf-8");
- $redis = new Redis ();
- $result = $redis->connect (' 10.10.10.119 ', 6379);
- $mywatchkey = $redis->get ("Mywatchkey");
- $rob _total = 100; //Number of snapped up
- if ($mywatchkey <$rob _total) {
- $redis->watch ("Mywatchkey");
- $redis->multi ();
- //Set delay for easy test effect.
- Sleep (5);
- //Insert snapping data
- $redis->hset ("Mywatchlist","User_id_". Mt_rand (1, 9999), Time ());
- $redis->set ("Mywatchkey",$mywatchkey + 1);
- $rob _result = $redis, exec ();
- if ($rob _result) {
- $mywatchlist = $redis->hgetall ("mywatchlist");
- echo "snapped up success! <br/> ";
- echo "remaining quantity:". ( $rob _total-$mywatchkey-1). "<br/>";
- echo "user list:<pre>";
- Var_dump ($mywatchlist);
- }else{
- echo "Bad luck, then snapped!" "; exit;
- }
- }
- ?>
Redis uses watch to complete the second kill snapping function (GO)