Memcache学习笔记一:Memcache在Linux下的安装和使用
标签(空格分隔): Memcache
一、下载Memcache源码包
二、安装Memcache 源码编译安装
- gcc依赖 可在线安装yum install gcc
lib-event 可在线安装yum install lib-event
含有文件: libevent libevent-doc libevent-headers libevent-devel
手动安装:进入lib-event目录,执行: rpm -ivh libevent-*
编译安装memcache
[root@mo memcached-1.4.31]# ./configure [root@mo memcached-1.4.31]# make
成功之后文件夹中会出现一个memcached的可执行文件
启动
//查看memcache的启动命令 [root@mo memcached-1.4.31]# ./memcached -help //启动命令: [root@mo memcached-1.4.31]#./memcached -p 11211 -vvv -u root
三、Memcache的API操作
连接Memecache驱动包:spymemcached-x.x.x.jar,xmemcached-x.x.x.jar, memcache-x.x.x.jar
使用较多的是xmemcached-x.x.x.jar
官网下载xmemcached-1.4.3-bin-with-dependencies.tar.gz
解压:含有用户指南和xmemcached-x.x.x.jar和依赖jar
导入jar包
xmemcached-1.4.3.jar slf4j-log4j12-1.6.1.jar slf4j-api-1.6.1.jar log4j-1.2.16.jar
API操作
import net.rubyeye.xmemcached.*; import net.rubyeye.xmemcached.command.BinaryCommandFactory; import net.rubyeye.xmemcached.exception.MemcachedException; import net.rubyeye.xmemcached.utils.AddrUtil; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.Date; import java.util.concurrent.TimeoutException; /** * Created by MOTUI on 2016/10/19. * * xmemcached操作Memcache */ public class TestCache { private MemcachedClient client; @Before public void before() throws IOException { //获得XMemcachedClientBuilder对象 XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.0.167:11211")); //添加二进制协议 builder.setCommandFactory(new BinaryCommandFactory()); //获得MemcachedClient client = builder.build(); } /** * 添加 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testAdd() throws InterruptedException, MemcachedException, TimeoutException { //100秒 boolean flag = client.add("user:1", 100, "zhangsan"); //boolean flag = client.add("user:2", 100, "zhangsan"); System.out.println(flag); } /** * 获取 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testGet() throws InterruptedException, MemcachedException, TimeoutException { Object object = client.get("user:1"); //Object object = client.get("user:2"); System.out.println(object); } /** * 保存或修改 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testSaveOrUpdate() throws InterruptedException, MemcachedException, TimeoutException { boolean flag = client.set("user:1", 1000, "wangwu"); System.out.println(flag); } /** * 更新 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testUpdate() throws InterruptedException, MemcachedException, TimeoutException { boolean flag = client.replace("user:1", 1000, new Date()); System.out.println(flag); } /** * 删除 * @throws TimeoutException * @throws InterruptedException * @throws MemcachedException */ @Test public void testDelete() throws TimeoutException, InterruptedException, MemcachedException{ boolean flag = client.delete("user:1"); System.out.println(flag); } /** * Memcached是通过cas协议实现原子更新,所谓原子更新就是compare and set * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testCAS() throws InterruptedException, MemcachedException, TimeoutException { GetsResponse<Object> response = client.gets("user:2"); //版本号 long version = response.getCas(); System.out.println(version); //修改 boolean flag = client.cas("user:2", 100, "update-user:2", version); System.out.println(flag); } @Test public void testCAS1() throws InterruptedException, MemcachedException, TimeoutException { boolean flag = client.cas("user:1", new CASOperation<Object>() { @Override public int getMaxTries() { return 1; } @Override public Object getNewValue(long l, Object o) { return "update-user:1"; } }); System.out.println(flag); } /** * 修改时间不修改值 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testTouch() throws InterruptedException, MemcachedException, TimeoutException { boolean flag = client.touch("user:1", 1000); System.out.println(flag); } /** * 需要开启二进制协议 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testGetAndTouch() throws InterruptedException, MemcachedException, TimeoutException { Object object = client.getAndTouch("user:1", 1000); System.out.println(object); } /** * 设置命名空间 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testWithNameSpace() throws InterruptedException, MemcachedException, TimeoutException { client.beginWithNamespace("com.motui.test"); boolean flag = client.set("user:01", 100, "nameSpace"); System.out.println(flag); Object object = client.get("user:01"); System.out.println(object); } @Test public void testWithNameSpace01() throws InterruptedException, MemcachedException, TimeoutException { Object object = client.withNamespace("com.motui.test2", new MemcachedClientCallable<Object>() { @Override public Object call(MemcachedClient memcachedClient) throws MemcachedException, InterruptedException, TimeoutException { client.set("user:01", 1000, "test2-nameSpace"); Object object = client.get("user:01"); return object; } }); System.out.println(object); } /** * 清除指定命名空间的缓存 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void testClearWithNameSpace() throws InterruptedException, MemcachedException, TimeoutException { client.invalidateNamespace("com.motui.test"); } /** * 清除所有缓存 * @throws InterruptedException * @throws MemcachedException * @throws TimeoutException */ @Test public void clearAll() throws InterruptedException, MemcachedException, TimeoutException { client.flushAll(); } }
四、Memcache和Redis的比较和应用场景
Memcache :分布式缓存服务 key-value 基于内存 最大缓存时长30天 key-value 最大 不得超过1M Memcache单节点服务器不支持集群. key String value:Object Redis :基于内存数据库 数据持久化 基于内存 key-value 缓存 key-value 最大 1GB Redis 支持集群 (主从/副本集+分片) key String value:String/List/Set(SortedSet)/Map SETEX(key,value,expire) Memcache使用场景: 1.分布式cache <--> 数据库静默 查 2.分布式session管理 提升应用服务器的健壮性 减轻服务器的内存压力
五、Memcache的分布
Memcached的分布是通过客户端实现的,客户端根据key的哈希值得到将要存储的memcached节点,
并将对应的value存储到相应的节点。
启动另一个Memcache:
[root@mo memcached-1.4.31]# ./memcached -p 11212 -vvv -u root
对于集群的连接只需要修改连接代码即可,因为xmemcached已经帮我们做好了连接的解决方案。修改如下:
import net.rubyeye.xmemcached.*;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;
/**
* Created by MOTUI on 2016/10/19.
*
* xmemcached操作Memcache
*/
public class TestCache01 {
private MemcachedClient client;
@Before
public void before() throws IOException {
//获得XMemcachedClientBuilder对象
//多个[ip]:[port]中间用空格分割
//一旦集群搭建完成,多个[ip]:[port]的顺序不能随意改变,顺序改变会导致数据无法取到
XMemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.0.167:11211 192.168.0.167:11212"));
//添加二进制协议
builder.setCommandFactory(new BinaryCommandFactory());
//获得MemcachedClient
client = builder.build();
}
/**
* Memcache集群测试
* @throws TimeoutException
* @throws InterruptedException
* @throws MemcachedException
*/
@Test
public void testCluster() throws TimeoutException, InterruptedException, MemcachedException{
for(int i=0;i<100;i++){
//插入数据
//client.set("key"+i, 1000, "value"+i);
//查数据
String key="key"+i;
int dbNum=Math.abs(key.hashCode())%2;
System.out.println("机器编号:"+dbNum+" > "+client.get(key));
}
}
}
结果可自行测试。
注意:
多个[ip]:[port]中间用空格分割
一旦集群搭建完成,多个[ip]:[port]的顺序不能随意改变,顺序改变会导致数据无法取到
其他配置参考xmemcached官方使用文档。在官方下载的.zip包下。