/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.aviation.utils;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*LRU 缓存map
* @author gaoxingliang
*/
public class LRUMap extends LinkedHashMap {
private int maxSize=20;
public LRUMap(int size){
super(size,0.75f,true);
this.maxSize=size;
};
protected boolean removeEldestEntry(Map.Entry eldest) {
return (size()>maxSize);
}
}
这个是偶然在阅读hibernate源码时发现的
LRUMap可以方便的用于缓存实现
代码如下:(只需要继承LinkedHashMap 覆盖一方法即可)

本文介绍了一种简单的LRU(Least Recently Used)缓存实现方式,通过继承Java中的LinkedHashMap并覆写特定的方法来轻松创建LRU缓存。这种方式不仅易于理解而且非常实用。
333

被折叠的 条评论
为什么被折叠?



