基于Java实现缓存Cache的深入分析
程序员文章站
2024-02-11 21:53:16
原理是使用linkedhashmap来实现,当缓存超过大小时,将会删除最老的一个元组。实现代码如下所示复制代码 代码如下:import java.util.linkedha...
原理是使用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实现缓存Cache的深入分析
-
PHP基于文件存储实现缓存的方法,
-
利用java序列化实现基于文件的快速索引 博客分类: 干货 序列化与反序列化快速索引Java数据保存协议
-
缓存策略之LRU实现(基于双链表实现) 博客分类: 数据结构与算法分析 算法Cache数据结构TomcatiBATIS
-
PHP基于文件存储实现缓存的方法
-
JAVA中基于Map实现缓存工具类
-
Redis:基于Redis的秒杀方案,缓存秒杀模型,先到先得、随机拼运气式秒杀算法Java实现,秒杀限流算法,漏桶算法、令牌桶算法伪代码
-
java基于swing实现的五子棋游戏代码
-
基于spring-boot和docker-java实现对docker容器的动态管理和监控功能[附完整源码下载]
-
Java基于swing实现的弹球游戏代码