Javaによるローカルキャッシュのシンプル化
コンシステンシhashアルゴリズムを実現するために(アルゴリズムはhttps://blog.csdn.net/weixin_35971547/article/details/89427847)、ローカルキャッシュの実装クラスを簡単に作成し、参考にしてください.
LocalCache.java
CacheEntity.java
以上のコードがテクノロジーと最後まで役立つことを願っています.
LocalCache.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* @author hilbert.xu
* @date 2019/4/21
*/
public class LocalCache {
private static final Logger log = LoggerFactory.getLogger(LocalCache.class);
/**
*
*/
private static final int DEFAULT_CAPACITY = 512;
/**
*
*/
private static final int MAX_CAPACITY = 100000;
/**
*
*/
private static final int MONITOR_DURATION = 2;
//
static {
new Thread(new TimeoutTimerThread()).start();
}
//
private static class LocalCacheInstance{
private static final LocalCache INSTANCE=new LocalCache();
}
public static LocalCache getInstance() {
return LocalCacheInstance.INSTANCE;
}
private LocalCache() {
}
/**
* Map
*/
private static Map cache = new ConcurrentHashMap<>(DEFAULT_CAPACITY);
/**
* key-value
*
* @param key
* @param value
* @param expireTime , -1
* @return
*/
public boolean putValue(String key, T value, int expireTime) {
return putCloneValue(key, value, expireTime);
}
/**
* clone ,
*
* @param key
* @param value
* @param expireTime
* @return
*/
private boolean putCloneValue(String key, T value, int expireTime) {
try {
if (cache.size() >= MAX_CAPACITY) {
return false;
}
//
CacheEntity entityClone = clone(new CacheEntity(value, System.nanoTime(), expireTime));
cache.put(key, entityClone);
return true;
} catch (Exception e) {
log.error(" :{}", e.getMessage());
}
return false;
}
/**
*
*
* @param object
* @return
*/
private E clone(E object) {
E cloneObject = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
cloneObject = (E) ois.readObject();
ois.close();
} catch (Exception e) {
log.error(" :{}", e.getMessage());
}
return cloneObject;
}
/**
* key , null
*
* @param key
* @return
*/
public Object getValue(String key) {
return cache.get(key).getValue();
}
/**
*
*/
public void clear() {
cache.clear();
}
/**
*
*/
static class TimeoutTimerThread implements Runnable {
@Override
public void run() {
while (true) {
try {
TimeUnit.SECONDS.sleep(MONITOR_DURATION);
checkTime();
} catch (Exception e) {
log.error(" :{}", e.getMessage());
}
}
}
/**
*
*
* @throws Exception
*/
private void checkTime() throws Exception {
//
for (String key : cache.keySet()) {
CacheEntity tce = cache.get(key);
long timoutTime = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - tce.getGmtModify());
// : timoutTime
if (tce.getExpire() > timoutTime) {
continue;
}
log.info(" : " + key);
//
cache.remove(key);
}
}
}
}
CacheEntity.java
import java.io.Serializable;
/**
* @author hilbert.xu
* @date 2019/4/20
*/
public class CacheEntity implements Serializable {
private static final long serialVersionUID = -107853226360392750L;
/**
*
*/
private Object value;
/**
*
*/
private long gmtModify;
/**
*
*/
private int expire;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public long getGmtModify() {
return gmtModify;
}
public void setGmtModify(long gmtModify) {
this.gmtModify = gmtModify;
}
public int getExpire() {
return expire;
}
public void setExpire(int expire) {
this.expire = expire;
}
public CacheEntity(Object value, long gmtModify, int expire) {
super();
this.value = value;
this.gmtModify = gmtModify;
this.expire = expire;
}
}
以上のコードがテクノロジーと最後まで役立つことを願っています.