Just saw some code in the queue of memcached dealing with snapping, I understand that there are two ways of thinking
1. Use the $mem->set (lock_key,1) to lock, $mem->delete (lock_key) unlocked after the queue ends
2. Use memcached's increment (key,1) to get the queue position
My question is:
1. Is the first method feasible?
2. $memcached->increment (key,1) does this function handle concurrent access? That is, multiple processes call the increment function at the same time, will there be concurrent overwrite problems?
I am a senior year, in the constantly looking for work, due to the lack of systematic php before learning, by YY and 4399 are the time of the second-round brush.
Heart is very cold, find work difficult, and the line and cherish, I want to good refueling, thank you Daniel pull me a
Reply to discussion (solution)
Conflict will occur
You can test it yourself.
Only with memcached you can conflict, for high concurrency conflicts, you can use Memcacheq to solve.
Memcacheq is a middleware developed specifically for the door to solve high concurrency problems, accessing data in a queue.
Reference: http://blog.csdn.net/fdipzone/article/details/17933673
1. The first method is possible, but the lock efficiency is too low, multiple client access is waiting to unlock, super slow (according to my test, 800 clients, each client Access 10 times, filling a 200 queue, it takes 32 seconds).
How this queue is implemented there's already a big God sticker on GitHub.
https://github.com/meetrajesh/php-memqueue/blob/master/memqueue.php
Only the 95th row while ($this->memc->add ($this->lock_key, ' 1 ', 0, 2)) is added while inside! To properly lock.
All operations of 2.memcached are atomic (get/set/increment, see Http://blog.csdn.net/jarfield/article/details/4336035#what's The big Benefit for any this), that is to say, no matter how many operations you throw into the memcached server, are executed sequentially, so method 2 is feasible. I also use 800 clients, each client Access 10 times, and finally view the value of increment, exactly 8000, so will not overwrite.
Coincidentally, I was also this year's senior seniors have been 4399,yy and other companies brush off.
Learn from the experience, slowly re-study it
Forget to say, queue service, recommended with Redis bar, he has a queue of various operations, performance than memcached on the server lock is much better.
http://redis.io/