memcached搭建

任务:在现有的项目中添加memcache以减少数据库压力,就先搭建了一个测试环境

系统:Ubuntu14

步骤:memcached依赖libevent包,我这个系统已经有了,就没有安装

安装memcached : apt-get install memcached

修改配置: vim /etc/memcached.conf 将里面的 -l 127.0.0.1注释掉(不注释代表只能本机访问)

启动memcached: /etc/init.d/memcached start

测试: telnet localhost 11211 提示 connected to memcached成功

项目中用的是xmemcached,把jar包导入项目,编写测试代码:
public static void main(String[] args) {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("192.168.64.129:11211"));
MemcachedClient memcachedClient = null;
try {
memcachedClient = builder.build();
memcachedClient.set("hello", 0, "Hello,xmemcached");
String value = memcachedClient.get("hello");
System.out.println("hello=" + value);
memcachedClient.delete("hello");
value = memcachedClient.get("hello");
System.out.println("hello=" + value);
} catch (MemcachedException e) {
System.err.println("MemcachedClient operation fail");
e.printStackTrace();
} catch (TimeoutException e) {
System.err.println("MemcachedClient operation timeout");
e.printStackTrace();
} catch (InterruptedException e) {
// ignore
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// close memcached client
memcachedClient.shutdown();
} catch (IOException e) {
System.err.println("Shutdown MemcachedClient fail");
e.printStackTrace();
}
}

输出:略
下一步是要建立一个双机高可用的,准备用keepalived,到时候再写
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值