See php. netmanualzhmemcached. cas. phpmemcached's CAS is like this: first, castoken is actually a version number. 1. When I get a piece of data, a version number ($ casToken) will be returned to me: $ data $ m-& amp; gt; get (& #039; ip_block & #039;, nu... see http://php.net/manual/zh/memcached.cas.php
Memcached's CAS principle is as follows:
First, the cas token is actually a version number,
1. When I retrieve a piece of data, a version number ($ casToken) will be returned to me:$data = $m->get('ip_block', null, $casToken);
2. After I modify $ data and save this $ data, I need this version number to save it back:$m->cas($casToken, 'ip_block', $data);
If $ data is already stored between steps 1 and 2, step 2 will fail because the version $ casToken has changed.
With CAS, you can ensure that the data is up-to-date and avoid repeated problems due to concurrency when competing for limited resources. For example, I have one left for sale, however, three people place orders at the same time.
Is there anything similar to mysql.
// Supplement
A pessimistic lock on a transaction cannot solve this problem, for example:
A. obtain data ~~~~~~~~~~~~~~~~~~~~~~~~~~ B. obtain data.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
After A operates on data, the transaction stores it with A pessimistic lock ~~~~~~ B System sleep
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ B. The system continues sleep.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ After B operates on data, the transaction stores it with A pessimistic lock, and the modification of A is overwritten.
Reply content:
See http://php.net/manual/zh/memcached.cas.php
Memcached's CAS principle is as follows:
First, the cas token is actually a version number,
1. When I retrieve a piece of data, a version number ($ casToken) will be returned to me:$data = $m->get('ip_block', null, $casToken);
2. After I modify $ data and save this $ data, I need this version number to save it back:$m->cas($casToken, 'ip_block', $data);
If $ data is already stored between steps 1 and 2, step 2 will fail because the version $ casToken has changed.
With CAS, you can ensure that the data is up-to-date and avoid repeated problems due to concurrency when competing for limited resources. For example, I have one left for sale, however, three people place orders at the same time.
Is there anything similar to mysql.
// Supplement
A pessimistic lock on a transaction cannot solve this problem, for example:
A. obtain data ~~~~~~~~~~~~~~~~~~~~~~~~~~ B. obtain data.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
After A operates on data, the transaction stores it with A pessimistic lock ~~~~~~ B System sleep
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ B. The system continues sleep.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ After B operates on data, the transaction stores it with A pessimistic lock, and the modification of A is overwritten.
Apply a pessimistic lock to a transaction, that is
Start transactionselect... for update // lock data... update... // update the lock data status commit
The above is the mechanism of mysql itself, and you can also use your own optimistic lock, that is
Adds a version field to the database.
select ...,verison as last_version form table where condition...update table set ...,version=version+1 where condition and version = last_version
For selection, see database concurrency control. Do you choose optimistic or pessimistic locks?