一、CAS协议
Memcached于1.2.4版本新增CAS(Check and Set)协议类,处理同一item被多个线程更改过程的并发问题。
在Memcached中,每个key关联有一个64-bit长度的long型惟一数值,表示该key对应value的版本号。这个数值由Memcached server产生,从1开始,且同一Memcached server不会重复。
在两种情况下这个版本数值会加1:①、新增一个key-value对;②、对某已有key对应的value值更新成功。
注:删除item版本值不会减小。重启Memcached server后,item版本值重新从1开始。
二、gets方法和cas方法
memcached基于hashmap存储,有get和set方法。但这并不能解决并发的问题。
下面操作可有效解决多线程并发冲突试图修改当前key-value对的value时,先由gets方法得到item的版本号,操作完成提交数据时,使用cas方法谨慎变更,如果发现版本号不对,则放弃修改。
三、实例
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 TestMemcached {
/**
* @param args
*/
public static void main(String[] args) {
try {
MemcachedClient cached = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
cached.set("x", 1800, "aaa");
CASValue x1 = cached.gets("x");
CASValue x2 = cached.gets("x");
System.out.println("CAS x1: " + x1.getCas());
System.out.println("value x1: " + x1.getValue());
System.out.println("");
System.out.println("CAS x2: " + x2.getCas());
System.out.println("value x2: " + x2.getValue());
System.out.println("");
CASResponse rx2 = cached.cas("x", x2.getCas(), "bbb");
System.out.println("response x2: " + rx2.toString());
CASValue x3 = cached.gets("x");
System.out.println("CAS x3: " + x3.getCas());
System.out.println("value x3: " + x3.getValue());
System.out.println("");
CASResponse rx1 = cached.cas("x", x1.getCas(), "ccc");
System.out.println("response x1: " + rx1.toString());
CASValue x4 = cached.gets("x");
System.out.println("CAS x4: " + x4.getCas());
System.out.println("value x4: " + x4.getValue());
System.out.println("");
System.out.println("laster cax : " + cached.gets("x").getCas());
cached.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
CAS x1: 9
value x1: aaa
CAS x2: 9
value x2: aaa
response x2: OK
CAS x3: 10
value x3: bbb
response x1: EXISTS
CAS x4: 10
value x4: bbb
laster cax : 10