Personal summary:To put it simply, CAS (check and set) is a mechanism to ensure concurrency consistency and belongs to the "optimistic lock" category. The principle is very simple: Take the version number, operate, compare the version number, and operate if it is consistent, if the operation is inconsistent, discard any operation. After the operation, add the version number to 1.
Lenovo has another mechanism, which is similar to this: Anti-repeated submission
1. maketoken: the server generates a token and uploads it to the page, that is, the version number (this value remains the same as long as the page is not refreshed );
2. checkvalid: Compare the token sent from the page with the token in the server when submitting the request. If the token is the same, submit the request. If the token is different, discard the request;
3. maketoken: update the token in the server.
The principle of struts1 is similar to that of struts2, except that the former requires developers to manually call the corresponding function, and the latter can use the interceptor for more concise completion.
---------- References ------------------
Reference: http://langyu.iteye.com/blog/680052
What is Cas?
The CAS (check and set) protocol added in memcached version 1.2.4 is similar to the concurrent CAS (compare and swap) Atomic operations in Java to handle the concurrency of the change process of the same item by multiple threads.
In memcached, each key is associated with a 64-bit long-type unique value, indicating the version number of the value corresponding to the key. This value is generated by the memcached server and starts from 1, and the same memcached server does not repeat. In both cases, the value of this version is and a key-value pair is added. 2. The value corresponding to an existing key is updated successfully. The version value of the deleted item does not decrease.
For example
Java code
Memcachedclient client = new memcachedclient ();
Client. Set ("fkey", "fvalue ");
// For the first set, the version number of the value corresponding to the fkey will be maintained in the memcached server, which is assumed to be 548;
Client. Set ("fkey", "svalue ");
// Set again, then the version number of the value corresponding to this fkey is 549;
Casvalue = client. Gets ("fkey ");
// You can obtain the CAS version number and actual value of the corresponding key (each memcached client has a similar object representation, and the name may be different, but the effect is similar), such as casvalue. getvalue = "svalue", casvalue. getcas = 549;
Problems solved by CAS Protocol
Simulate the scenario where multiple memcached clients concurrently set the same key. For example, clienta wants to set the value of the current key to "X" and the operation is successful. clientb overwrites the value of the current key from "X" to "Y ", at this time, clienta obtains "Y" instead of the expected "X" when getting the value based on the key. It uses this value, but does not know that this value has been modified by other threads, the problem may occur.
CAS protocol solves the concurrent modification problem. When a thread tries to modify the value of the current key-value pair, the gets method first obtains the version number of the item. When the operation completes data submission, the CAS method is used for careful modification, if this key-value pair has been changed by other threads on the memcached server during the local operation on the item, the modification will be abandoned (optimistic lock concept ).
Java code
Casvalue = client. Gets (key );
//*****
// Local processing
//*****
Casresponse response = client. CAS (Key, newvalue, casvalue );
// When I retrieve data, the version number of the item is casvalue. getcas (), so I expect that the version number of the item has not changed during submission. If the version number is not the version number when I retrieve the data, the memcached server does not submit anything this time, if the return value is true or false, the user puts forward the solution (do nothing or get the version number again, and submit again)
Correctness verification in Concurrent Environments
Use multiple memcached clients to change the same key value concurrently and increase the value. If the number of operations-cas failure COUNT = value increases, CAS processing is normal in the concurrent environment.
Java code
Import java. Io. ioexception;
Import java.net. inetsocketaddress;
Import net. Spy. memcached. casresponse;
Import net. Spy. memcached. casvalue;
Import net. Spy. memcached. memcachedclient;
Public class castest {
Private Static memcachedclient client = NULL;
Static {
Try {
Client = new memcachedclient (
New inetsocketaddress ("localhost", 11211 ));
} Catch (ioexception O ){
O. printstacktrace ();
}
}
Public static void main (string [] ARGs) throws exception {
// Firstly, the key shoshould exist.
// Key is "Number", value is integer 1, 7845 is expire time
Client. Set ("Number", 7845, 1 );
Castest testobj = new castest ();
// Start the multithread Environment
For (INT I = 0; I <10; I ++ ){
Testobj. New threadtest ("thread-" + (I + 1). Start ();
}
}
/**
* Each thread runs times
*/
Private class threadtest extends thread {
Private memcachedclient client = NULL;
Threadtest (string name) throws ioexception {
Super (name );
Client = new memcachedclient (
New inetsocketaddress ("localhost", 11211 ));
}
Public void run (){
Int I = 0;
Int success = 0;
While (I <10 ){
I ++;
Casvalue <Object> uniquevalue = client. Gets ("Number ");
Casresponse response = client. CAS ("Number ",
Uniquevalue. getcas (), (integer) uniquevalue. getvalue () + 1 );
If (response. tostring (). Equals ("OK ")){
Success ++;
}
System. Out. println (thread. currentthread (). getname () + "" + I
+ "Time" + "Update oldvalue:" + uniquevalue
+ ", Result:" + response );
}
If (success <10 ){
System. Out. println (thread. currentthread (). getname ()
+ "Unsuccessful times:" + (10-success ));
}
}
}
}
The results of each execution are different. For example, if the result of a certain execution is 100 operations in total, 47 conflicts, and the value increases from 1 to 53, the verification is successful.
If you have any shortcomings, please submit them!