memcached
来源:互联网 发布:数字摇奖软件 编辑:程序博客网 时间:2024/06/04 00:50
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");
}
}
阅读全文
0 0
- Memcached
- memcached
- Memcached
- memcached
- Memcached
- memcached
- memcached
- memcached
- memcached
- memcached
- Memcached
- memcached
- memcached
- memcached
- Memcached
- memcached
- memcached
- memcached
- ACM常识总结
- ubuntu 16.04 安装FTP服务器
- Node.js —— Waterline的介绍和使用
- 两个运算命令 expr bc
- 滤波
- memcached
- Python处理xml文件
- 51Nod
- framebuffer 应用层函数
- HDU 4970 Killing Monsters(树状数组 || 思维)
- IP 地址聚合 经典算法 已经过验证
- Scheduler模块详解
- FreeMarker日期打印不出来
- 113. Path Sum II