标签:
1 import java.io.Serializable; 2 import java.text.DateFormat; 3 import java.util.Date; 4 import java.util.Map; 5 6 import com.danga.MemCached.MemCachedClient; 7 import com.pt.util.memcached.MemcachedTool; 8 9 10 public class testMemcached { 11 12 /** 13 * @param args 14 */ 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 MemCachedClient mc = MemcachedTool.getInstance(); 18 //(1000 * 129是本地时间和服务器端时间的差值) 19 Date failDate = new Date(System.currentTimeMillis() - 1000 * 129 + 10000); 20 System.out.println(failDate); 21 mc.set("c", "230",failDate); //如果有相同的key值,则替换掉原先的值 失效时间(以服务器的时间为准) 22 Boolean add = mc.add("b", "110"); //如果存在同样key值,则返回false 23 String str = (String)mc.get("c"); 24 String[] keys = {"a","b"}; 25 Map strs = mc.getMulti(keys); //获取多个值 返回结果是一个数组 26 System.out.println("get value from memcached: " + str); 27 mc.delete("c"); //删除key 28 29 System.out.println("test add method: " + add); 30 System.out.println("get params: " + strs); 31 32 //写入对象 33 user men = new user(); 34 men.setId(1907); 35 men.setName("潘腾"); 36 boolean setObj = mc.set("user", men); 37 user getMen = (user)mc.get("user"); 38 System.out.println(getMen); 39 } 40 41 } 42 43 class user implements Serializable{ 44 String name; 45 int id; 46 public String getName() { 47 return name; 48 } 49 public void setName(String name) { 50 this.name = name; 51 } 52 public int getId() { 53 return id; 54 } 55 public void setId(int id) { 56 this.id = id; 57 } 58 @Override 59 public String toString() { 60 // TODO Auto-generated method stub 61 return "name: " + name + " id: " + id; 62 } 63 64 }
1 package com.pt.util.memcached; 2 3 import com.danga.MemCached.MemCachedClient; 4 import com.danga.MemCached.SockIOPool; 5 6 public class MemcachedTool { 7 private static MemCachedClient memcacheClient = null; 8 9 private MemcachedTool(){ 10 11 } 12 13 public static MemCachedClient getInstance(){ 14 if(memcacheClient == null){ 15 memcacheClient = new MemCachedClient(); 16 memcacheClient.setDefaultEncoding("UTF-8"); //写入缓存的编码格式 17 18 } 19 return memcacheClient; 20 } 21 22 static{ 23 String[] serversArray = {"192.168.65.221:13220"}; 24 Integer[] weight = {1}; 25 SockIOPool connPool = SockIOPool.getInstance(); 26 connPool.setServers(serversArray); //设置memcached服务器 27 connPool.setWeights(weight); //设置各个服务器存储权重 28 connPool.setMinConn(3); //设置连接池的最小连接数目 29 connPool.setInitConn(3); //初始化可用连接数目 30 connPool.setMaxIdle(10000); //可用连接池最长等待时间 31 connPool.setSocketTO(10000); //读取等待超时值 32 connPool.setSocketConnectTO(10000); //连接等待超时值 33 //心跳检测,设置为true时,每次通信都会先检测连接是否可用 增加IO和网络开销,一般设置为false 默认是false 34 connPool.setAliveCheck(false); 35 /** 36 * alg=0 使用String.hashCode()获得hash code,该方法依赖JDK,可能和其他客户端不兼容,建议不使用 37 * alg=1 使用original 兼容hash算法,兼容其他客户端 38 * alg=2 使用CRC32兼容hash算法,兼容其他客户端,性能优于original算法 39 * alg=3 使用MD5 hash算法 40 * 采用前三种hash算法的时候,查找cache服务器使用余数方法。采用最后一种hash算法查找cache服务时使用consistent方法。 41 **/ 42 connPool.setHashingAlg(3); 43 //设置服务器宕机或连接由中断变为恢复后,该连接继续可用 44 connPool.setFailback(true); 45 //启动pool 46 connPool.initialize(); 47 } 48 }
需要引入:java-memcached-release_2.5.2.jar包
标签:
原文地址:http://www.cnblogs.com/tengpan-cn/p/5396243.html