标签:memcached
Memcached实际上就是两次Hash算法结合本地缓存
接下来我们使用第一种
这是需要的jar
package com.chengxi.memc.test; import org.junit.Test; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcachedTest01 { @Test public void testOne() throws Exception { MemCachedClient client = new MemCachedClient(); //mem服务器地址 String[] addr = {"192.168.0.140:11211"}; //对应的权重 Integer[] weight = {3}; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(addr); pool.setWeights(weight); pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(200); pool.setMaxIdle(1000*30*30); pool.setMaintSleep(30); //socket param timeout pool.setNagle(false); pool.setSocketTO(30); pool.setSocketConnectTO(0); //start pool.initialize(); //client.set("name", "wzh"); //System.out.println(client.get("name")); Student student = new Student(); student.setId(1); student.setName("呵呵"); client.set("student1",student); System.out.println(client.get("student1")); } @Test public void two(){ MemCachedClient client = new MemCachedClient(); //mem服务器地址 String[] addr = {"192.168.0.140:11211","192.168.0.140:11212"}; //对应的权重 Integer[] weight = {5,5}; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(addr); pool.setWeights(weight); pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(200); pool.setMaxIdle(1000*30*30); pool.setMaintSleep(30); //socket param timeout pool.setNagle(false); pool.setSocketTO(30); pool.setSocketConnectTO(0); //start pool.initialize(); for(int i = 0;i<10;i++){ client.set("test"+i,"test"+i); } } }
官方的jar包实现了 哈希一致性
也就是说 上面的 two的方法 分别将test0-9 存进了两台mem服务器中
如果其中一台宕机了 获取数据的时候不会影响另外一台
如果没有实现一致哈希的话 就会影响其他服务器 导致所有的数据无法获取
package com.chengxi.memc.test; import java.io.Serializable; public class Student implements Serializable { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "[id:"+this.id+",name"+this.name+"]"; } }
标签:memcached
原文地址:http://blog.csdn.net/cjaver/article/details/41207565