memcached学习笔记2(CAS协议)

一、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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值