使用 LinkedHashMap(访问有序)的 LRU 缓存实现。这还通过注册/订阅此类事件的调用者通知回调的返回值即时执行收缩和扩展。
代码被很好地注释以详细说明实现。
package com.javaTutorialProject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
private int maxEntries;
private static final int DEFAULT_INITIAL_CAPACITY = 10;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private ArrayList<LRUCacheOverflowNotify> subscribers = new ArrayList<>();
public LRUCache(LRUCacheOverflowNotify subscriber,int initialCapacity,
float loadFactor,
int maxEntries) {
super(initialCapacity, loadFactor, true);
this.maxEntries = maxEntries;
subscribe(subscriber);
}
public LRUCache(LRUCacheOverflowNotify subscriber, int initialCapacity,
int maxEntries) {
this(subscriber, initialCapacity, DEFAULT_LOAD_FACTOR, maxEntries);
}
public LRUCache(LRUCacheOverflowNotify subscriber, int maxEntries) {
this(subscriber, DEFAULT_INITIAL_CAPACITY, maxEntries);
}
// not very useful constructor
public LRUCache(LRUCacheOverflowNotify subscriber, Map<? extends K, ? extends V> m,
int maxEntries) {
this(subscriber, m.size(), maxEntries);
putAll(m);
}
private void subscribe(LRUCacheOverflowNotify subscriber){
if(subscriber != null) subscribers.add(subscriber);
}
@Override
protected boolean removeEldestEntry(Map.Entry Head) {
if(size() > maxEntries){ // if overflow (we handle it)
int savedMaxEntries = maxEntries, newMaxEntries; // get lowestMin/highestMax entries from all subscribers
for(LRUCacheOverflowNotify subscriber: subscribers){ // notify all subscribers
newMaxEntries = subscriber.onNotifyHeadRemoval(Head); // receive opinion (shrink/expand/re-use)
if(newMaxEntries > maxEntries && newMaxEntries > savedMaxEntries) { // if new > max and new > last max expand
savedMaxEntries = newMaxEntries; // save it
continue;
}
if(newMaxEntries < maxEntries && newMaxEntries < savedMaxEntries) { // if new < max and new < last min shrink
savedMaxEntries = newMaxEntries; // Head will be removed by // save it
}
}
if(savedMaxEntries > 0 && savedMaxEntries < maxEntries) { // if 0 < saved < max Shrink, reqSize-1(we already added 1)
Iterator<K> iterator = this.keySet().iterator(); // initialize iterator
try {
while ((this.size() - 1) >= savedMaxEntries && iterator.hasNext()) {// if size >= shrinked_size and have next() try remove
iterator.next(); // prime it for iterator(else IllegalStateException)
iterator.remove(); // remove LRU element from LinkedHashMap
}
}catch (IllegalStateException e){
e.printStackTrace(); // record Error stackTrace
}
maxEntries = this.size(); // re-initialize maxEntries count
return false; // don't flush Head(LRU)
}
if(savedMaxEntries > maxEntries){ // if saved > max Expand,
maxEntries = savedMaxEntries; // max = saved
return false; // don't flush Head(LRU)
}
return true; // if saved == max || saved < 0 , flush LRU entry (Head)
}
return false;
}
public interface LRUCacheOverflowNotify{
int onNotifyHeadRemoval(Map.Entry Head);
}
}
使用此 LRU 缓存实现的测试类:
package com.javaTutorialProject;
import java.util.Map;
import java.util.Random;
public class TestLRUCache implements LRUCache.LRUCacheOverflowNotify {
static int size = 7;
static int count = 0;
public static void main(String[] args) {
LRUCache<Integer,String> linkedHashMap = new LRUCache<Integer, String>(new TestLRUCache(), 5,0.75f, size);
for(int i = 1; i < 35; i++){
linkedHashMap.put(i,String.valueOf(i));
System.out.println("Last inserted item: " + i);
System.out.println("LRU Cache size: " + linkedHashMap.size());
System.out.println("LRU Cache current: "+ linkedHashMap);
// random access(to check LRU implementation)
System.out.println("Last accessed item: " + linkedHashMap.get(new Random(System.currentTimeMillis()).nextInt(i)));
}
}
@Override
public int onNotifyHeadRemoval(Map.Entry Head) {
System.out.println("Count: " + ++count);
if(count==2) size -=2;
if(count==5) size +=2;
if(count==10) size -= 2;
if(count==15) size += 2;
if(count==20) size -= 2;
return size;
}
}