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());
}
}
参考:http://www.linuxidc.com/Linux/2011-12/49516.htm
Memcached基础的更多相关文章
- Memcached基础介绍
1.memcached是什么,有什么作用? )memcached是一个开源的.高性能的内存的缓存软件,从名称上看mem就是内存的意思,而cache就是缓存的意思. )memcached通过在事先规划好 ...
- 分布式缓存技术memcached学习(二)——memcached基础命令
上文<linux环境下编译memcahed>介绍了memcahed在linux环境下的安装以及登录,下面介绍memcahed的基本命令的使用. Add 功能:往内存增加一条新的缓存记录 语 ...
- Memcached基础知识
主要内容: Memcached基本的工作原理 Memcached的两阶段哈希 Memcached的数据存储方式 Memcached新建Item分配内存过程 Memcached的数据过期方式 Memca ...
- Memcached 笔记与总结(1)Linux(CentOS 6.6) 和 Windows(7)下安装与配置 Memcached (1.4.24)与 Memcached 基础命令
Memcached 官方网站:http://memcached.org/ 官网对其的描述是: What is Memcached? Free & open source, high-perfo ...
- (转)实战Memcached缓存系统(1)Memcached基础及示例程序
1.Cache定义 (1)狭义概念:用于CPU的相对高速处理与主存(Main Memory)的相对低速处理的之间起到协调功能的硬件设备. (2)广义概念:用于速度相差较大的两种硬件之间,起到协调两者数 ...
- 分布式缓存技术memcached学习系列(二)——memcached基础命令
上文<linux环境下编译memcahed>介绍了memcahed在linux环境下的安装以及登录,下面介绍memcahed的基本命令的使用. Add 功能:往内存增加一条新的缓存记录 语 ...
- 【 学习笔记 】memcached基础知识
源地址:http://kb.cnblogs.com/page/42731/ 仔细学习了下,以下是记录的笔记备忘内容. 一.memcached是什么? memcached是高性能的分布式内存缓存服 ...
- memcached完全剖析系列——一、memcached基础
转自:http://blog.charlee.li/memcached-001/
- Memcached目录
Memcached 简介.安装和基本使用 Memcached基础知识 理解Memcached的分布式 Memcached存储命令 - set Memcached存储命令 - add Memcached ...
随机推荐
- google自定义站内搜索
ttps://www.google.com/cse/docs/cref.html?hl=zh-cn 重要表单参数: action 字段:您希望存储结果的网址(在该例中,我们使用 http://www. ...
- apache pk nginx pk Lighttpd
apache: 历史: APACHE:于1994年发布,是apache软件基金会的一个开放源码的网页服务器,可以在多平台下运行,由于其多平台和安全性被广泛使用,是最流行的web服务器端软件之一:特点是 ...
- ecshop二次开发之购物车常见问题
1.ecshop二次开发中保存注册用户购物车数据解决方法:ecshop购物车是数据库中cart表来支持的,在ecshop表中rec_id是编号,user_id是注册用户的id,session_id表示 ...
- TCP/IP的三次握手协议
关于TCP/IP的三次握手协议,这篇文章中有详细的介绍,很通俗易懂,什么时候忘了,都可以过来瞧两眼,保证很快就明白了. 首先TCP/IP协议分为三个阶段:建立连接(握手阶段),数据传输阶段,连接终止阶 ...
- HTTP POST和GET的区别[转]
http://www.cppblog.com/woaidongmao/archive/2008/05/29/51476.aspx 1.HTTP 只有POST和GET 两种命令模式: 2.POST是被设 ...
- struts2中#,$,%的用法以及el,ognl表达式的用法
OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例 取Session中的值 <c:out value="${sessionScope.user.userId}&quo ...
- J2SE知识点摘记(二十六)
为了用“集合框架”的额外部分把排序支持添加到 Java 2 SDK,版本 1.2,核心 Java 库作了许多更改.像 String 和 Integer 类如今实现 Comparable 接口以提供自然 ...
- jQuery 之 $.get、$.post、$.getJSON、$.ajax
对 JSTL标签 加以巩固,同时在自己的博客中与之分享: http://app.yinxiang.com/shard/s20/sh/76feadb0-957a-40bc-895d-4d28025ce2 ...
- 扩展C++ string类
在实际开发过程中,C++string类使用起来有很多不方便的地方,笔者根据根据这些不足简单的扩展了这个类,如增加与数字之间的相互转化和格式化字符串.不足的地方望指正.读者也可以根据自己需求继续扩展. ...
- 在VirtualBox虚拟机上采集Fedora15系统
在VirtualBox虚拟机上采集Fedora15系统 第一部分:创建系统并磁盘分区 1.点击VirtualBox上的新建 2.添加名称,选择类型和版本,点下一步 3.写入内存(不要超过物理内存),点 ...