基于Java实现缓存Cache的深入分析
更新时间:2013年06月04日 11:58:32 作者:
本篇文章是对Java实现缓存Cache进行了详细的分析介绍,需要的朋友参考下
原理是使用LinkedHashMap来实现,当缓存超过大小时,将会删除最老的一个元组。
实现代码如下所示
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache {
public static class CachedData {
private Object data = null;
private long time = 0;
private boolean refreshing = false;
public CachedData(Object data) {
this.data = data;
this.time = System.currentTimeMillis();
}
public Object getData() {
return data;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean getRefreshing() {
return refreshing;
}
public void setRefreshing(boolean b) {
this.refreshing = b;
}
}
protected static class CacheMap extends LinkedHashMap {
protected int maxsize = 0;
public CacheMap(int maxsize) {
super(maxsize * 4 / 3 + 1, 0.75f, true);
this.maxsize = maxsize;
}
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > this.maxsize;
}
}
protected CacheMap map = null;
public LRUCache(int size) {
this.map = new CacheMap(size);
}
public synchronized void set(Object key, Object value) {
map.remove(key);
map.put(key, new CachedData(value));
}
public synchronized void remove(Object key) {
map.remove(key);
}
public synchronized CachedData get(Object key) {
CachedData value = (CachedData) map.get(key);
if (value == null) {
return null;
}
map.remove(key);
map.put(key, value);
return value;
}
public int usage() {
return map.size();
}
public int capacity() {
return map.maxsize;
}
public void clear() {
map.clear();
}
}
实现代码如下所示
复制代码 代码如下:
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache {
public static class CachedData {
private Object data = null;
private long time = 0;
private boolean refreshing = false;
public CachedData(Object data) {
this.data = data;
this.time = System.currentTimeMillis();
}
public Object getData() {
return data;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean getRefreshing() {
return refreshing;
}
public void setRefreshing(boolean b) {
this.refreshing = b;
}
}
protected static class CacheMap extends LinkedHashMap {
protected int maxsize = 0;
public CacheMap(int maxsize) {
super(maxsize * 4 / 3 + 1, 0.75f, true);
this.maxsize = maxsize;
}
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > this.maxsize;
}
}
protected CacheMap map = null;
public LRUCache(int size) {
this.map = new CacheMap(size);
}
public synchronized void set(Object key, Object value) {
map.remove(key);
map.put(key, new CachedData(value));
}
public synchronized void remove(Object key) {
map.remove(key);
}
public synchronized CachedData get(Object key) {
CachedData value = (CachedData) map.get(key);
if (value == null) {
return null;
}
map.remove(key);
map.put(key, value);
return value;
}
public int usage() {
return map.size();
}
public int capacity() {
return map.maxsize;
}
public void clear() {
map.clear();
}
}
相关文章
JavaSE的三大接口:Comparator,Comparable和Cloneable详解
这篇文章主要介绍了详解JavaSE中Comparator,Comparable和Cloneable接口的区别的相关资料,希望通过本文大家能彻底掌握这部分内容,需要的朋友可以参考下2021-10-10Java多线程之readwritelock读写分离的实现代码
这篇文章主要介绍了Java多线程之readwritelock读写分离的相关内容,文中涉及具体实例代码,具有一定参考价值,需要的朋友可以了解下。2017-10-10Java Swing JToggleButton开关按钮的实现
这篇文章主要介绍了Java Swing JToggleButton开关按钮的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12Java @Async注解导致spring启动失败解决方案详解
这篇文章主要介绍了Java @Async注解导致spring启动失败解决方案详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08Springboot使用Logback实现日志配置与异常记录
默认情况下,SpringBoot内部使用logback作为系统日志实现的框架,将日志输出到控制台,不会写到日志文件。本篇文章主要讲解下如何自定义logabck.xml以及对logback文件中配置做一个详解,需要的可以参考一下2022-11-11
最新评论