Memcached基础
1、实例化
MemcachedClient client = new XMemcachedClient(); public XMemcachedClient() public XMemcachedClient(List<InetSocketAddress> addressList) public XMemcachedClient(final String server, final int port) public XMemcachedClient(final String host, final int port, int weight) public XMemcachedClient(final InetSocketAddress inetSocketAddress, int weight) public XMemcachedClient(final InetSocketAddress inetSocketAddress) ...
最简单的一个就是最后一个,比如
MemcachedClient cache = new XMemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
2、常用方法
2、1、add
public <T> boolean add(final String key, final int exp, final T value, final Transcoder<T> transcoder, final long timeout) public <T> boolean add(final String key, final int exp, final T value, final Transcoder<T> transcoder) public boolean add(final String key, final int exp, final Object value,final long timeout) public boolean add(final String key, final int exp, final Object value)
第一个参数:键(key)。
第二个参数:过期时间(单位是秒)。
第三个参数:要设置缓存中的对象(value),如果没有则插入,如果有则修改。
第四个参数:通信编码方式。
第五个参数:超时时间。
2.2、set
public final <T> boolean set(String key, final int exp, final T value, final Transcoder<T> transcoder, final long timeout) public final <T> boolean set(final String key, final int exp, final T value, final Transcoder<T> transcoder) public final boolean set(final String key, final int exp, final Object value, final long timeout) public final boolean set(final String key, final int exp, final Object value)
第一个参数:键(key)。
第二个参数:过期时间(单位是秒)。
第三个参数:要设置缓存中的对象(value),如果没有则插入,如果有则修改。
第四个参数:通信编码方式。
第五个参数:超时时间。
2.3、replace
public <T> boolean replace(final String key, final int exp, final T value, final Transcoder<T> transcoder, final long timeout) public <T> boolean replace(final String key, final int exp, final T value,final Transcoder<T> transcoder) public boolean replace(final String key, final int exp, final Object value,final long timeout) public boolean replace(final String key, final int exp, final Object value)
第一个参数:键(key)。
第二个参数:过期时间(单位是秒)。
第三个参数:要设置缓存中的对象(value),如果没有则插入,如果有则修改。
第四个参数:通信编码方式。
第五个参数:超时时间。
2.4、get
public <T> T get(final String key, final long timeout, final Transcoder<T> transcoder) public <T> T get(final String key, final long timeout) public <T> T get(final String key, final Transcoder<T> transcoder) public <T> T get(final String key)
第一个参数:键(key)
第二个参数:过期时间(单位是秒)
第三个参数:通信编码方式。
3、CAS(Check And Save)协议
基本原理就是:版本号,防止读写冲突。涉及到两个方法gets 和cas
3.1、gets
public <T> GetsResponse<T> gets(final String key, final long timeout, final Transcoder<T> transcoder) public <T> GetsResponse<T> gets(final String key, final Transcoder transcoder) public <T> GetsResponse<T> gets(final String key, final long timeout) public <T> GetsResponse<T> gets(final String key)
参数同get方法,其中GetsResponse只有2个字段,一个value,另一个变量cas就是时间戳。
public final class GetsResponse<T> { private final long cas; private final T value; ... //get and set // equals and hascode }
3.2、cas
public boolean cas(final String key, final int exp, final Object value,final long timeout, final long cas) public boolean cas(final String key, final int exp, final Object value,final long cas) public <T> boolean cas(final String key, final int exp, final T value,final Transcoder<T> transcoder, final long cas) public <T> boolean cas(final String key, final int exp,final CASOperation<T> operation, final Transcoder<T> transcoder) public <T> boolean cas(final String key, final int exp,GetsResponse<T> getsReponse, final CASOperation<T> operation,final Transcoder<T> transcoder) public <T> boolean cas(final String key, final int exp,GetsResponse<T> getsReponse, final CASOperation<T> operation) public <T> boolean cas(final String key, GetsResponse<T> getsResponse,final CASOperation<T> operation) public <T> boolean cas(final String key, final int exp,final CASOperation<T> operation) public <T> boolean cas(final String key, final CASOperation<T> operation) public <T> boolean cas(final String key, final int exp, final T value,final Transcoder<T> transcoder, final long timeout, final long cas)
4、例子
4。1、非cas方式
import net.spy.memcached.MemcachedClient; public class Test { public static void main(String[] args) throws IOException { MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); cache.set("x", 1800, "Love"); String obj1 = (String) cache.get("x"); String obj2 = (String) cache.get("x"); obj2 = "Michael"; cache.set("x", 1800, obj2); System.out.println("Non-CAS 2:\t" + obj2); System.out.println("Non-CAS 1:\t" + obj1); } }
obj1!=obj2,出现读写不一致
4.2、使用cas方式
import java.io.IOException; import java.net.InetSocketAddress; import net.spy.memcached.CASValue; import net.spy.memcached.MemcachedClient; public class Test { @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); cache.set("y", 1800, "Love"); CASValue casValue1 = cache.gets("y"); CASValue casValue2 = cache.gets("y"); cache.cas("y", casValue2.getCas(), casValue2.getValue()); System.out.println("CAS 2:\t" + casValue2.getCas()); System.out.println("Value 2:\t" + casValue2.getValue()); System.out.println("CAS 1:\t" + casValue1.getCas()); System.out.println("Value 1:\t" + casValue1.getValue()); } }