1,下载安装第三方源:
CentOS 6 或 RedHat 6 安装此rpm , 其他 下载 :http://pkgs.repoforge.org/rpmforge-release/
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
rpm -ivh rpmforge-release-0.5.2-2.el6.rf.i686.rpm
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
2,安装memcahce.
yum install memcached
3,启动服务.
service memcached start
将服务设置 为自动启动:
chkconfig memcached on
4,测试客户端.(需要安装 nc)
yum install nc
具体命令参考:[引用]
http://blog.s135.com/post/384/
1、数据存储(假设key为zhangyan,value为12345)
STORED
2、数据取回(假设key为zhangyan)
VALUE zhangyan 0 5
12345
END
3、数值增加1(假设key为zhangyan,并且value为正整数)
12346
4、数值减少3(假设key为zhangyan,并且value为正整数)
12343
5、数据删除(假设key为zhangyan)
DELETED
6、查看Memcached状态
STAT pid 3025
STAT uptime 4120500
STAT time 1228021767
STAT version 1.2.6
STAT pointer_size 32
STAT rusage_user 433.463103
STAT rusage_system 1224.515845
STAT curr_items 1132460
STAT total_items 8980260
STAT bytes 1895325386
STAT curr_connections 252
STAT total_connections 547850
STAT connection_structures 1189
STAT cmd_get 13619685
STAT cmd_set 8980260
STAT get_hits 6851607
STAT get_misses 6768078
STAT evictions 0
STAT bytes_read 160396238246
STAT bytes_written 260080686529
STAT limit_maxbytes 2147483648
STAT threads 1
END
7、模拟top命令,查看Memcached状态:
或者
5.使用java 客户端连接memcache服务器.
继续添加虚拟机端口.
同时修改 iptables 配置文件.
重启 iptables
service iptables restart
5,java 工程 demo.
package com.test.demo; import java.io.IOException; import java.net.InetSocketAddress; import net.spy.memcached.MemcachedClient; public class MemcachedDemo { public static void main(String[] args) throws IOException { long t1 = System.currentTimeMillis(); MemcachedClient memcachedClient = new MemcachedClient( new InetSocketAddress("127.0.0.1", 11211)); // 可以设置多个..new InetSocketAddress("127.0.0.1", 11211) for (int i = 0; i < 10000; i++) { UserInfo userInfo = new UserInfo(); userInfo.setUserName("name" + i); userInfo.setPasswd("passwd" + i); userInfo.setAge(i % 99); // 更新后被替换.. memcachedClient.add("user_" + i, 3600, userInfo); } System.out.println("finish."); // 删除key memcachedClient.delete("user_0"); memcachedClient.delete("user_1"); memcachedClient.delete("user_2"); // 按照key得到数据. for (int i = 0; i < 10000; i++) { if (i % 1000 == 0) { Object userInfo = memcachedClient.get("user_" + i); System.out.println(i + "\t" + userInfo); } } for (int i = 3; i < 1000; i++) { // 删除数据 memcachedClient.delete("user_" + i); } // 一定用完毕要关闭. memcachedClient.shutdown(); long t2 = System.currentTimeMillis(); System.out.println("Program has executed for " + (int) ((t2 - t1) / 1000) + " s " + ((t2 - t1) % 1000) + " ms "); } }
执行结果:
有的时候会报错误.是因为一次取数据太多了.超时了.
Exception in thread "main" net.spy.memcached.OperationTimeoutException: Timeout waiting for value at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:1142) at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:1157) at com.test.demo.MemcachedDemo.main(MemcachedDemo.java:35) Caused by: net.spy.memcached.internal.CheckedOperationTimeoutException: Timed out waiting for operation - failing node: /127.0.0.1:11211 at net.spy.memcached.internal.OperationFuture.get(OperationFuture.java:65) at net.spy.memcached.internal.GetFuture.get(GetFuture.java:37) at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:1135) ... 2 more
是在测试的时候 循环了 10000 次造成的吧..
也可.在 hibernate 里面配置 二级缓存 为 memcache .
附件:
java 工程下载.
下载java_src