java自定义LRU缓存,Java自定义LRU缓存实现

自定义LRU缓存实现 无 public interface Cache {/** * 缓存ID * @return */String getId(); Object get(String key);void put(String key,Object value);void clear();void remove(String key);int size();} public class LRUCacheImpl implements Cache {pri

自定义LRU缓存实现

public interface Cache {

/**

* 缓存ID

* @return

*/

String getId();

Object get(String key);

void put(String key,Object value);

void clear();

void remove(String key);

int size();

}

public class LRUCacheImpl implements Cache {

private long timeout=0; //0代表永不超时

private Map cache=null;

private String id;

public LRUCacheImpl(String id,int maxsize,long timeout){

this.timeout=timeout;

this.cache=new LRULinkedHashMap(maxsize);

this.id=id;

}

public LRUCacheImpl(int maxsize,long timeout){

this.timeout=timeout;

this.cache=new LRULinkedHashMap(maxsize);

this.id=UUID.randomUUID().toString();

}

@Override

public String getId() {

// TODO Auto-generated method stub

return this.id;

}

@Override

public Object get(String key) {

// TODO Auto-generated method stub

Object value=null;

CacheEntry entry;

synchronized (this) {

entry=cache.get(key);

}

if(entry!=null){

//如果没有超时,或者超时设置为0

if((System.currentTimeMillis()-entry.getTimeCached())

value=entry.getValue();

}else{

cache.remove(key);

}

}

return value;

}

@Override

public synchronized void put(String key, Object value) {

// TODO Auto-generated method stub

cache.put(key, new DefaultCacheEntry(value, System.currentTimeMillis()));

}

@Override

public synchronized void clear() {

// TODO Auto-generated method stub

cache.clear();

}

@Override

public synchronized void remove(String key) {

// TODO Auto-generated method stub

cache.remove(key);

}

@Override

public int size() {

// TODO Auto-generated method stub

return cache.size();

}

}

public class LRULinkedHashMap extends LinkedHashMap{

private int maxSize;

public LRULinkedHashMap(int maxsize){

super(maxsize/2+1,0.75f,true);

this.maxSize=maxsize;

}

/*

* 当缓存数超过最大容量时,返回true把最后一个移除。

* @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry)

*/

@Override

protected boolean removeEldestEntry(Map.Entry eldest) {

return size()>maxSize;

}

}

public interface CacheFactory {

Cache createCache(String id,int maxsize,long timeout);

}

public class LRUCacheFactory implements CacheFactory{

@Override

public Cache createCache(String id, int maxsize, long timeout) {

// TODO Auto-generated method stub

return new LRUCacheImpl(id,maxsize,timeout);

}

}

public interface CacheEntry {

/**

* 加入缓存时的时间

* @return

*/

long getTimeCached();

Object getValue();

}

public class DefaultCacheEntry implements CacheEntry{

private long timecached=-1;

private Object value;

public DefaultCacheEntry(Object value,long timecached){

this.value=value;

this.timecached=timecached;

}

@Override

public long getTimeCached() {

// TODO Auto-generated method stub

return timecached;

}

@Override

public Object getValue() {

// TODO Auto-generated method stub

return value;

}

}

public class CacheManager{

private static final ConcurrentHashMap caches=new ConcurrentHashMap<>();

/**

* 缓存策略枚举

*/

public static enum CacheStrategy{

LRU

}

/**

*

* @param maxsize

* @param timeout

* @param strategy 可为null。默认LRU

* @return

*/

public static Cache createCache(int maxsize, long timeout,CacheStrategy strategy) {

if(strategy==null){

strategy=CacheStrategy.LRU;

}

Cache cache=getCacheFactory(strategy).createCache(UUID.randomUUID().toString(),maxsize, timeout);

caches.putIfAbsent(cache.getId(), cache);

return cache;

}

private static CacheFactory getCacheFactory(CacheStrategy strategy){

CacheFactory factory=null;

if("LRU".equals(strategy.name())){

factory=new LRUCacheFactory();

}

return factory;

}

public static Cache getCacheById(String cacheId){

return caches.get(cacheId);

}

public static void clear(){

caches.clear();

}

public static void remove(String cacheId){

caches.remove(cacheId);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值