MemCached的学习使用

大名鼎鼎的memcached 不用多做介绍,下面是引用memcached全面剖析中的介绍。(附件中有全文 强烈推荐)
memcached是什么?
memcached 是以LiveJournal 旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。现在
已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示。但随着数
据量的增大、访问的集中,就会出现RDBMS的负担加重、数据库响应恶化、网站显示延迟等重大
影响。
这时就该memcached大显身手了。memcached是高性能的分布式内存缓存服务器。一般的使用目的
是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展
性。
这里我使用的是window版的memcached 1.2.1
客户端采用java的 XMemcached 1.2.5
附件中都提供下载。
安装memcached非常简单
‘c:\memcached\memcached.exe -d install’ 安装
‘c:\memcached\memcached.exe -p 11211 -m 16 -d start’ 启动
memcached的基本设置:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

Xmemcached需要依赖slf4j这个日志包
由于memcached使用的时候太简单了 所以这里只给出简单测试 至于add set replace的区别 还是建议大家去看memcached全面剖析
测试

MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("localhost:11211"));
@Test
@Ignore
public void testSet(){
try {
MemcachedClient 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 (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
}
@Test
@Ignore
public void testReplaceAndAdd(){
try {
MemcachedClient memcachedClient = builder.build();
memcachedClient.replace("hello", 0, "hello.world!");
System.out.println("hello = "+memcachedClient.get("hello"));
memcachedClient.add("hello", 0, "hello,world!");
System.out.println("hello = "+memcachedClient.get("hello"));
memcachedClient.add("hello", 0, "hello,world! update");
System.out.println("hello = "+memcachedClient.get("hello"));
memcachedClient.set("hello", 0, "hello,world! update");
System.out.println("hello = "+memcachedClient.get("hello"));
memcachedClient.delete("hello");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
}
@Test
@Ignore
public void test(){
MemcachedClientBuilder mcb = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211"));
try {
MemcachedClient mclient = mcb.build();
mclient.set("a", 0, "aaa");
mclient.set("b", 0, "bbb");
mclient.set("c", 0, "ccc");
Collection<String> collection = new ArrayList<String>();
collection.add("a");
collection.add("b");
collection.add("c");
Map<String, String> aa = mclient.get(collection);
Iterator<String> iter = aa.keySet().iterator();
while(iter.hasNext()){
String value = iter.next();
System.out.println(value+" = "+aa.get(value));
}
mclient.shutdown();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值