用JAVA实现简单的Memcached功能
慎用,一般不提倡占用应用服务器的内存。
MemcacheMapVO.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* MemcacheMap 保存的对象
*
* @author Herbert
*
*/
public class MemcacheMapVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2719454259953108119L;
/**
* value
*/
private Object value;
/**
* 过期时间
*/
private Long expires;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Long getExpires() {
return expires;
}
public void setExpires(Long expires) {
this.expires = expires;
}
public int size() {
try {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(buf);
o.writeObject(this.value);
byte[] bytes = buf.toByteArray();
return bytes.length * 8;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
MemcacheMapUtil.java
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;
import com.netease.photo.share.meta.vo.MemcacheMapVO;
/**
* 用JAVA实现简单的<a href="http://www.danga.com/memcached/"target="_blank"title="Memcached" >Memcached</a>功能<br>
*
* @author Herbert
*
*/
public final class MemcacheMapUtil {
private MemcacheMapUtil() {
}
public static final int MINUTES = 60;
/**
* MAP个数
*/
private static final long MAXSIZE = 3000;
/**
* 安全个数
*/
private static final long SAFESIZE = 2800;
/**
* 最大时间
*/
private static final int MAXTIME = 24 * 60 * 60 * 1000;
/**
* 存储的值
*/
private static HashMap<String, MemcacheMapVO> memcacheMap = new HashMap<String, MemcacheMapVO>();
/**
* 得到值
*
* @param key
*
* @return
*/
public static Object get(final String key) {
MemcacheMapVO memcacheMapVO = memcacheMap.get(key);
long now = new Date().getTime();
// 判断是否有这个值存在
if (memcacheMapVO == null) {
return null;
// 判断保存的值是否过期
} else if (now >= memcacheMapVO.getExpires()) {
memcacheMap.remove(key);
return null;
} else {
return memcacheMapVO.getValue();
}
}
/**
* 删除值
*
* @param key
*/
public static void delete(final String key) {
memcacheMap.remove(key);
}
/**
*
* @param key
* 对象的KEY
* @param value
* 对象的值
* @param seconds
* 对象的有效时间
*/
public static void set(final String key, final Object value, final int seconds) {
// 清理工作
int count = memcacheMap.size();
if (count <= MemcacheMapUtil.SAFESIZE) {
// 小于安全个数,不需要清理
} else if (count > MemcacheMapUtil.SAFESIZE) {
// 大于安全个数,按时间清理
checkMemcacheMap();
if (count >= MemcacheMapUtil.MAXSIZE) {
// 清理后无效,进行强制清理
clearMemcacheMap();
}
}
// 增加值的工作
MemcacheMapVO memcacheMapVO = new MemcacheMapVO();
memcacheMapVO.setValue(value);
long now = new Date().getTime();
long expires = seconds * 1000;
if (expires >= MAXTIME) {
expires = MAXTIME;
}
expires += now;
memcacheMapVO.setExpires(expires);
memcacheMap.put(key, memcacheMapVO);
}
/**
* 默认保存时间为12小时
*
* @param key
* @param value
*/
public static void set(final String key, final Object value) {
set(key, value, 12 * 60 * 60);
}
/**
* 产生一个KEY
*
* @param methodName
* 方法名
* @param parameters
* 参数列表
*/
public static String createKey(String methodName, Object... parameters) {
String str = methodName;
for (Object obj : parameters) {
str += "_" + obj.toString();
}
return str;
}
/**
* 强制清理到安全值
*/
private static void clearMemcacheMap() {
// 清除
long clearCount = MemcacheMapUtil.MAXSIZE - MemcacheMapUtil.SAFESIZE;
String[] keys = (String[]) memcacheMap.keySet().toArray();
int keyCount = keys.length;
Random random = new Random();
for (int i = 0; i < clearCount; i++) {
int randomIndex = random.nextInt(keyCount);
String key = keys[randomIndex];
delete(key);
}
}
/**
* 检查过期的对象
*/
private static void checkMemcacheMap() {
// 这种迭代的方式节约了一次迭代
Iterator<Entry<String, MemcacheMapVO>> iterator = memcacheMap.entrySet().iterator();
long now = new Date().getTime();
while (iterator.hasNext()) {
Map.Entry<String, MemcacheMapVO> entry = (Map.Entry<String, MemcacheMapVO>) iterator.next();
String key = (String) entry.getKey();
MemcacheMapVO memcacheMapVO = entry.getValue();
if (now >= memcacheMapVO.getExpires()) {
memcacheMap.remove(key);
}
}
}
}
| Print article | This entry was posted by Herbert on 2010/05/19 at 18:50, and is filed under Architecture. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |