結合本地緩存java
接下來咱們使用第一種 算法
這是需要的jarspring
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(); //memserver地址 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(); //memserver地址 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 存進了兩臺memserver中框架
假設當中一臺宕機了 獲取數據的時候不會影響另一臺socket
假設沒有實現一致哈希的話 就會影響其它server 致使所有的數據沒法獲取ide
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