linux下安装和配置memcached,以及java操作的示例代码

最近项目中,想用memcached来做分布式缓存。于是学习了一下基本的配置很安装。
具体过程如下:

Linux 安装和配置memcached
1、 下载libevent,因为memcached用到了libevent的类库来处理socket
2、 Libevent下载地址:http://libevent.org/,选个新的稳定版,如2.0.18

[img]http://dl.iteye.com/upload/attachment/0067/1347/7a677e25-3d24-3d38-8bf0-d115c9fa1228.jpg[/img]

3、 下载Memcached,地址为http://memcached.org,当前最新版为1.4.13

[img]http://dl.iteye.com/upload/attachment/0067/1349/d49b1ff3-b072-3452-9c53-6dc7b9290337.jpg[/img]

4、 将这两个文件上传到linux上:

[img]http://dl.iteye.com/upload/attachment/0067/1351/c33d0576-7112-33d2-b91e-dfc6c4aa54eb.jpg[/img]

5、 登录服务器并安装两个软件:

[img]http://dl.iteye.com/upload/attachment/0067/1353/33605553-8037-3cf0-9264-aef91e5ce6a1.jpg[/img]

先安装libevent-2.0.18-stable.tar.gz

安装前检查系统是否已经安装有,如果有的话删除旧版本后再安装:
检查命令:
rpm –qa|grep libevent


[img]http://dl.iteye.com/upload/attachment/0067/1355/a09639d7-ae8e-3dc7-8b83-66374d098dc2.jpg[/img]

删除旧版本:
rpm -e libevent-1.1a-3.2.1 –nodeps,nodeps—忽略依赖删除

[img]http://dl.iteye.com/upload/attachment/0067/1357/331dc578-7398-34e6-b7af-551a74e08e3c.jpg[/img]

再检查后发现没有了
[img]http://dl.iteye.com/upload/attachment/0067/1359/cce2d396-2951-3373-855c-0e750b62b3af.jpg[/img]

[code="java"]# tar zxvf libevent-2.0.18-stable.tar.gz
# cd libevent-2.0.18-stable
# ./configure –prefix=/usr/local/libevent // 指定安装路径
….
….[/code]
执行一些配置工作,往后在编译
[code="java"]#make
……
……[/code]
编译往后再安装
[code="java"]# make install
make install-recursive
……
make[1]: Leaving directory `/opt/softwares/libevent-2.0.18-stable'
# [/code]

[img]http://dl.iteye.com/upload/attachment/0067/1361/570a7c1a-2ccf-38a0-9e24-da6f0f679ef9.jpg[/img]

说明安装完毕

[img]http://dl.iteye.com/upload/attachment/0067/1363/b74dbd44-7248-34fc-a98b-7beadb52f217.jpg[/img]

检查是否安装成功,没有检查出来,呵呵?本人linux不是很熟悉,不知道何故,但是不影响,只要编译和安装过程没有报错就行了!

[img]http://dl.iteye.com/upload/attachment/0067/1365/5c7335ff-29e4-3309-9acc-baf93a76fcfc.jpg[/img]

6、 安装memcached
[code="java"]# tar zvxf memcachaed-1.4.13.tar.gz
……
memcachaed-1.4.13/compile
# [/code]

[img]http://dl.iteye.com/upload/attachment/0067/1367/5cf1af6f-dab2-3e3d-8ba0-f8192f3d407c.jpg[/img]

配置:
./configure-prefix=/usr/local/memcached-with-libevent

或者如果出错要制定libevent的路径
./configure-prefix=/usr/local/memcached-with-libevent –with-libevent=/usr/local/libevent


[img]http://dl.iteye.com/upload/attachment/0067/1369/b0f9a519-0554-3cc2-b883-81226f464567.jpg[/img]

编译和安装
[code="java"]# make[/code]

[img]http://dl.iteye.com/upload/attachment/0067/1371/050ce303-992b-33dd-b3cd-58212d672398.jpg[/img]

[code="java"]# make install[/code]

[img]http://dl.iteye.com/upload/attachment/0067/1373/0b6f70fc-f4b3-3c8f-a7aa-7c8d636560ea.jpg[/img]

……

[img]http://dl.iteye.com/upload/attachment/0067/1375/79dcf99c-afa7-3ee3-8b71-939a79c3c1d0.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/0067/1377/40f0f8e3-007e-3bae-9585-a8cbd67cb5b3.jpg[/img]

7、 运行:
# ./memcached -uroot
如果用root用户运行必须要带-u参数指明运行的用户是谁

[img]http://dl.iteye.com/upload/attachment/0067/1379/1a8f6101-01e9-30dd-93e1-f2ceadf9f766.jpg[/img]

8、 测试:
9、 Telnet 172.30.0.1 11211

[img]http://dl.iteye.com/upload/attachment/0067/1381/b22e5a24-885d-368e-a7e7-22ffc4215299.jpg[/img]

说明已经连上了,即memcached已经启动了。

[img]http://dl.iteye.com/upload/attachment/0067/1383/3b6c85f9-f4d8-3447-bc68-91d9ff7499b3.jpg[/img]

在黑屏下输入stats -- 可能看不到输入,不回显,就可以看到当前memcached的运行参数了:

[img]http://dl.iteye.com/upload/attachment/0067/1385/47057bbb-efe7-37ff-adfa-4785d72b361e.jpg[/img]

写个java 测试程序:
下载java的客户端lib:https://github.com/gwhalin/Memcached-Java-Client/downloads
[img]http://dl.iteye.com/upload/attachment/0067/1387/3b518563-eb18-339b-af6b-a224d85bbb55.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/0067/1389/30a52044-5b51-3bc3-8536-c592dd5df20d.jpg[/img]

Memcached的工具类,还有一些方法没有写出来,可以参考API,将所有的方法都写出来就可以了通用了.
/**
* MemcachedUtil.java V1.0 2012-4-25 上午09:54:51
*
* Copyright 2011 ************. All rights reserved.
*
* Modification history(By Time Reason):
*
* Description:
*/

package com.kevin.common.util.cache.memcached;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
*
* <pre><b>功能描述:</b>Memcached的工具类
*
* @author :****(Kevin.xie)<br>
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public final class MemcachedUtil {

/**
* <b>构造函数:工具类,禁止实例化</b>
*
*/
private MemcachedUtil() {

}

// 创建全局的唯一实例
private static MemCachedClient mcc = new MemCachedClient();

/**
* 自身实例
*/
private static MemcachedUtil memcachedUtil = new MemcachedUtil();

// 设置与缓存服务器的连接池
static {
// 服务器列表和其权重
String[] servers = {"172.30.0.1:11211" };// Ip地址和端口号
// 权重
Integer[] weights = {3 };

// 获取socket连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();

// 设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);

// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);

// 设置主线程的睡眠时间
pool.setMaintSleep(30);

// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);

// 初始化连接池
pool.initialize();

// 压缩设置,超过指定大小(单位为K)的数据都会被压缩
// mcc.setCompressEnable(true);
// mcc.setCompressThreshold(64 * 1024);

mcc.setPrimitiveAsString(true);// 设置序列化
}

/**
*
* <pre><b>功能描述:</b>获取唯一实例.
*
* @author :****(Kevin.xie)
* <b>创建日期 :</b>2012-4-25 上午10:57:41
*
* @return
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public static MemcachedUtil getInstance() {

return memcachedUtil;
}

/**
*
* <pre><b>功能描述:</b>新增一个缓存数据
*
* @author :****(Kevin.xie)
* <b>创建日期 :</b>2012-4-25 上午10:55:15
*
* @param key 缓存的key
* @param value 缓存的值
* @return 操作结果

*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public boolean add(String key, Object value) {

// 不会存入缓存
return mcc.add(key, value);
// return mcc.set(key, value);
}

/**
*
* <pre><b>功能描述:</b>新增一个缓存数据
*
* @author :****(Kevin.xie)
* <b>创建日期 :</b>2012-4-25 上午10:56:15
*
* @param key 缓存的key
* @param value 缓存的值
* @param expiry 缓存过期的时间
* @return 操作结果
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public boolean add(String key, Object value, Date expiry) {

// 不会存入缓存
return mcc.add(key, value, expiry);
// return mcc.set(key, value, expiry);
}

/**
* <pre><b>功能描述:</b>替换已有的缓存数据
*
* @author :****(Kevin.xie)
* <b>创建日期 :</b>2012-4-25 上午10:55:34
*
* @param key 设置对象的key
* @return Object 设置对象的值
* @return 是否替换成功
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public boolean replace(String key, Object value) {

return mcc.replace(key, value);
}

/**
*
* <pre><b>功能描述:</b>替换已有的缓存数据
*
* @author :****(Kevin.xie)
* <b>创建日期 :</b>2012-4-25 上午10:43:17
*
* @param key 设置对象的key
* @return Object 设置对象的值
* @param expiry 过期时间
* @return 是否替换成功
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public boolean replace(String key, Object value, Date expiry) {

return mcc.replace(key, value, expiry);
}

/**
*
* <pre><b>功能描述:</b>根据指定的关键字获取对象
*
* @author :****(Kevin.xie)
* <b>创建日期 :</b>2012-4-25 上午10:42:49
*
* @param key 获取对象的key
* @return Object 对象值
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public Object get(String key) {

return mcc.get(key);
}
}


测试的持久化对象,必须要序列化
/**
* Employee.java V1.0 2012-4-25 上午09:50:57
*
* Copyright 2011 ************. All rights reserved.
*
* Modification history(By Time Reason):
*
* Description:
*/

package com.kevin.memcached.model;

import java.io.Serializable;

/**
*
* <pre><b>功能描述:</b>员工类,必须要序列化,否则缓存操作时会报错的
*
* @author :****(Kevin.xie)<br>
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public class Employee implements Serializable {

/**
* serialVersionUID
*/
private static final long serialVersionUID = -6487397580836471048L;

/**
* 员工名字
*/
private String EmpName;

/**
* 部门名
*/
private String deptName;

/**
* 公司名
*/
private String companyName;

/**
*
* <b>构造函数:</b>
*
*/
public Employee() {

}

/**
* Access method for the empName property
*
* @return the empName
*/
public String getEmpName() {

return EmpName;
}

/**
* Sets the value of empName the property
*
* @param empName the empName to set
*/
public void setEmpName(String empName) {

EmpName = empName;
}

/**
* Access method for the deptName property
*
* @return the deptName
*/
public String getDeptName() {

return deptName;
}

/**
* Sets the value of deptName the property
*
* @param deptName the deptName to set
*/
public void setDeptName(String deptName) {

this.deptName = deptName;
}

/**
* Access method for the companyName property
*
* @return the companyName
*/
public String getCompanyName() {

return companyName;
}

/**
* Sets the value of companyName the property
*
* @param companyName the companyName to set
*/
public void setCompanyName(String companyName) {

this.companyName = companyName;
}
}


缓存的数据必须要序列化,否则会报错的:


[img]http://dl.iteye.com/upload/attachment/0067/1391/caadf31f-ef62-32e2-86b1-20d55949fda2.jpg[/img]

Juni测试类:MemcachedUtilTest
/**
* MemcachedUtilTest.java V1.0 2012-4-25 上午11:00:30
*
* Copyright 2011 ************. All rights reserved.
*
* Modification history(By Time Reason):
*
* Description:
*/

package com.kevin.memcached;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.kevin.common.util.cache.memcached.MemcachedUtil;
import com.kevin.memcached.model.Employee;

public class MemcachedUtilTest {

private Employee emp;
private MemcachedUtil cache;

@Before
public void setUp() throws Exception {

emp = new Employee();
emp.setCompanyName("Kevin's Company");
emp.setDeptName("R&D Dept");
emp.setEmpName("Kevin");

// 缓存对象
cache = MemcachedUtil.getInstance();
cache.add("emp", emp);

// 缓存基本数据
cache.add("Your salary", 12345.00);
}

@After
public void tearDown() throws Exception {

// cache.remove("emp");
// cache.remove("Your salary");
cache = null;
emp = null;
}

@Test
public void testAddCache() {

}

@Test
public void testGetCache() {

/**
* 缓存基本数据
*/
MemcachedUtil cache = MemcachedUtil.getInstance();

System.out.println("Your salary : " + cache.get("Your salary"));

Employee tempEmp = (Employee) cache.get("emp");
System.out.println("Company: " + tempEmp.getCompanyName());
System.out.println(" Dept: " + tempEmp.getDeptName());
System.out.println(" Name: " + tempEmp.getEmpName());

Assert.assertEquals(emp.getCompanyName(), tempEmp.getCompanyName());
}
}


[img]http://dl.iteye.com/upload/attachment/0067/1411/735ed3de-1018-3c87-a843-c64197da6fe9.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/0067/1393/becf19f3-ed72-30d7-a1b0-30e83a144d4a.jpg[/img]

/// 两个不同的客户端测试:

测试类:MemcachedTest
/**
* MemcachedTest.java V1.0 2012-4-24 下午05:50:08
*
* Copyright 2011 ************. All rights reserved.
*
* Modification history(By Time Reason):
*
* Description:
*/

package com.kevin.memcached;

import com.kevin.common.util.cache.memcached.MemcachedUtil;
import com.kevin.memcached.model.Employee;

/**
*
* <pre><b>功能描述:</b>MemcachedTest 测试添加对象缓存,比验证取出的缓存数据是否一致
*
* @author :****(Kevin.xie)<br>
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public class MemcachedTest {

public static void main(String[] args) {

MemcachedUtil cache = MemcachedUtil.getInstance();
Employee emp = new Employee();
emp.setCompanyName("Kevin's Company");
emp.setDeptName("R&D Dept");
emp.setEmpName("Kevin");

cache.add("emp", emp);

Employee tempEmp = (Employee) cache.get("emp");
System.out.println("Company: " + tempEmp.getCompanyName());
System.out.println(" Dept: " + tempEmp.getDeptName());
System.out.println(" Name: " + tempEmp.getEmpName());
}
}


[img]http://dl.iteye.com/upload/attachment/0067/1411/735ed3de-1018-3c87-a843-c64197da6fe9.jpg[/img]

测试类:MemcachedTest1
/**
* MemcachedTest1.java V1.0 2012-4-24 下午11:23:58
*
* Copyright 2011 ************. All rights reserved.
*
* Modification history(By Time Reason):
*
* Description:
*/

package com.kevin.memcached;

import com.kevin.common.util.cache.memcached.MemcachedUtil;
import com.kevin.memcached.model.Employee;

/**
*
* <pre><b>功能描述:</b>测试取出从MemcachedTest存入的数据是否一致
*
* @author :****(Kevin.xie)<br>
*
* <b>修改历史:</b>(修改人,修改时间,修改原因/内容)
*
* </pre>
*/
public class MemcachedTest1 {

public static void main(String[] args) {

MemcachedUtil cache = MemcachedUtil.getInstance();
cache.add("msg", "Say hello to you !");
System.out.println("get value : " + cache.get("msg"));

Employee tempEmp = (Employee) cache.get("emp");
System.out.println("Company: " + tempEmp.getCompanyName());
System.out.println(" Dept: " + tempEmp.getDeptName());
System.out.println(" Name: " + tempEmp.getEmpName());

}
}


[img]http://dl.iteye.com/upload/attachment/0067/1413/170603c0-ab3c-3be3-9920-a0d9a005701a.jpg[/img]

完毕,kevin.xie 2012.04.25
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值