Memcached的配置和使用
1、下载windows版本,64位下载地址: http://s3.amazonaws.com/downloads.northscale.com/memcached-win64-1.4.4-14.zip
2、使用cmd,进入解压后的memcached文件夹,执行memcached.exe -d install
再执行memcached.exe -d start 启动
以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
3、所需jar包
4、代码
MemcachedUtil
package com.memcached; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcachedUtil { // memcached客户端单例 private static MemCachedClient mcc = new MemCachedClient(); /** * 初始化连接池 */ static { // 获取连接池的实例 SockIOPool pool = SockIOPool.getInstance(); // 服务器列表及权重 String[] servers = { "127.0.0.1:11211" };// 里面可能是一系列的服务器 Integer[] weights = { 3 }; // 设置服务器信息 pool.setServers(servers); pool.setWeights(weights); // 设置初始连接数、最小连接数、最大连接数、最大处理时间 pool.setInitConn(10); pool.setMinConn(10); pool.setMaxConn(1000); pool.setMaxIdle(1000 * 60 * 60); //设置链接池守护线程的睡眠时间 pool.setMaintSleep(60); //设置TCP参数,连接超时 pool.setNagle(false); pool.setSocketTO(60); pool.setSocketConnectTO(0); //初始化并启动连接池 pool.initialize(); } private MemcachedUtil(){} public static boolean addCache(String key , Object value){ return mcc.add(key, value);//add方法不会替换原来的值 } public static boolean addCache(String key , Object value , Integer expire){ return mcc.add(key, value,expire); } public static boolean put(String key , String value){ return mcc.set(key, value);//set方法会替换原来的值 } public static void clean(){ mcc.flushAll(); } public static Object get(String key){ return mcc.get(key); } }
MemBean
package com.memcached; import java.io.Serializable; public class MemBean implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MemBean other = (MemBean) obj; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } @Override public String toString() { return "username:" + username + ",password:" + password; } }
MemTest
package com.memcached; import org.junit.Test; public class MemTest { @Test public void memTest(){ MemcachedUtil.addCache("hello", "helloworld"); MemcachedUtil.put("hello", "helloworld1"); String hello = (String) MemcachedUtil.get("hello"); System.out.println(hello); MemcachedUtil.clean(); String hello1 = (String) MemcachedUtil.get("hello"); System.out.println(hello1); } }