Redis uses watch to complete the second kill snapping function

Source: Internet
Author: User
Tags vars

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?
  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 snapped up
  7. if ($mywatchkey <$rob _total) {
  8. $redis->watch ("Mywatchkey");
  9. $redis->multi ();
  10. //Set delay for easy test effect.
  11. Sleep (5);
  12. //Insert snapping data
  13. $redis->hset ("Mywatchlist","User_id_". Mt_rand (1, 9999), Time ());
  14. $redis->set ("Mywatchkey",$mywatchkey + 1);
  15. $rob _result = $redis, exec ();
  16. if ($rob _result) {
  17. $mywatchlist = $redis->hgetall ("mywatchlist");
  18. echo "snapped up success!  <br/> ";
  19. echo "remaining quantity:". ( $rob _total-$mywatchkey-1).  "<br/>";
  20. echo "user list:<pre>";
  21. Var_dump ($mywatchlist);
  22. }else{
  23. echo "Bad luck, then snapped!" ";  exit;
  24. }
  25. }
  26. ?>

Redis uses watch to complete the second kill snapping function (GO)

Related Article

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.