package com.rthd.utils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.meetup.memcached.MemcachedClient;
import com.meetup.memcached.SockIOPool;
public class CacheUtils {
public static final Logger LOGGER = Logger.getLogger("CacheUtils");
private static final Map<String, Long> expires = new HashMap<String, Long>();
private static final MemcachedClient mcc = new MemcachedClient();
public static void init() throws Exception {
LOGGER.debug("init:start");
Properties props = new Properties();
props.load(CacheUtils.class
.getResourceAsStream("/cache_types.properties"));
for (Entry<Object, Object> entry : props.entrySet()) {
expires.put((String) entry.getKey(),
Long.parseLong((String) entry.getValue()));
}
props = new Properties();
props.load(CacheUtils.class
.getResourceAsStream("/memcached.properties"));
// 创建一个Socked连接池实例
SockIOPool pool = SockIOPool.getInstance();
// 向连接池设置服务器和权重
pool.setServers(props.getProperty("servers").split(","));
// 设置服务器权重
String[] weightStrs = props.getProperty("weights").split(",");
Integer[] weights = new Integer[weightStrs.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = Integer.parseInt(weightStrs[i]);
}
pool.setWeights(weights);
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle(Boolean.parseBoolean(props.getProperty("nagle", "false")));
pool.setSocketTO(Integer.parseInt(props.getProperty("socket_to", "3000")));
pool.setSocketConnectTO(0);
// initialize the connection pool
pool.initialize();
LOGGER.debug("init:end");
}
public static void put(String type, String key, Object value) {
try {
Long expire = expires.get(type);
Date expiry = new Date(expire == null ? 0L: expire);
mcc.set(type + '-' + key, value, expiry);
LOGGER.debug("put(" + type + "," + key + "," + value + ")");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void remove(String type, String key) {
try {
mcc.delete(type + '-' + key);
LOGGER.debug("remove(" + type + "," + key + ")");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object get(String type, String key) {
try {
LOGGER.debug("get(" + type + "," + key + ")");
return mcc.get(type + '-' + key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void destroy() throws Exception {
LOGGER.debug("destroy:start");
expires.clear();
LOGGER.debug("destroy:send");
}
public static void main(String[] args) throws Exception {
init();
put("xml", "test", "12345");
String str = (String) get("xml", "test");
System.out.println(str);
//remove("init", "ip");
}
}
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.meetup.memcached.MemcachedClient;
import com.meetup.memcached.SockIOPool;
public class CacheUtils {
public static final Logger LOGGER = Logger.getLogger("CacheUtils");
private static final Map<String, Long> expires = new HashMap<String, Long>();
private static final MemcachedClient mcc = new MemcachedClient();
public static void init() throws Exception {
LOGGER.debug("init:start");
Properties props = new Properties();
props.load(CacheUtils.class
.getResourceAsStream("/cache_types.properties"));
for (Entry<Object, Object> entry : props.entrySet()) {
expires.put((String) entry.getKey(),
Long.parseLong((String) entry.getValue()));
}
props = new Properties();
props.load(CacheUtils.class
.getResourceAsStream("/memcached.properties"));
// 创建一个Socked连接池实例
SockIOPool pool = SockIOPool.getInstance();
// 向连接池设置服务器和权重
pool.setServers(props.getProperty("servers").split(","));
// 设置服务器权重
String[] weightStrs = props.getProperty("weights").split(",");
Integer[] weights = new Integer[weightStrs.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = Integer.parseInt(weightStrs[i]);
}
pool.setWeights(weights);
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle(Boolean.parseBoolean(props.getProperty("nagle", "false")));
pool.setSocketTO(Integer.parseInt(props.getProperty("socket_to", "3000")));
pool.setSocketConnectTO(0);
// initialize the connection pool
pool.initialize();
LOGGER.debug("init:end");
}
public static void put(String type, String key, Object value) {
try {
Long expire = expires.get(type);
Date expiry = new Date(expire == null ? 0L: expire);
mcc.set(type + '-' + key, value, expiry);
LOGGER.debug("put(" + type + "," + key + "," + value + ")");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void remove(String type, String key) {
try {
mcc.delete(type + '-' + key);
LOGGER.debug("remove(" + type + "," + key + ")");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object get(String type, String key) {
try {
LOGGER.debug("get(" + type + "," + key + ")");
return mcc.get(type + '-' + key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void destroy() throws Exception {
LOGGER.debug("destroy:start");
expires.clear();
LOGGER.debug("destroy:send");
}
public static void main(String[] args) throws Exception {
init();
put("xml", "test", "12345");
String str = (String) get("xml", "test");
System.out.println(str);
//remove("init", "ip");
}
}