基于Java实现缓存Cache的深入分析
时间:2020-12-23 11:09:38|栏目:JAVA代码|点击: 次
原理是使用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();
}
}
栏 目:JAVA代码
下一篇:java计算时间差的方法
本文标题:基于Java实现缓存Cache的深入分析
本文地址:http://www.codeinn.net/misctech/35576.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机