快取淘汰演算法 —— LFU-Aging(Java實現)
阿新 • • 發佈:2018-12-09
Java實現,用HashMap儲存關係{key值 : 命中次數與上次命中時間},當需要淘汰某個key值時,呼叫map.remove(key)。
import java.util.*; public class LFUAgingMap<K, V> extends HashMap<K, V> { private static final int DEFAULT_MAX_SIZE = 3; private int maxSize = DEFAULT_MAX_SIZE; Map<K, HitRate> km = new HashMap<K, HitRate>(); public LFUAgingMap() { this(DEFAULT_MAX_SIZE); } public LFUAgingMap(int maxSize) { super(maxSize); this.maxSize = maxSize; } @Override public V get(Object key) { V v = super.get(key); if (v != null) { HitRate hitRate = km.get(key); hitRate.hitCount += 1; hitRate.atime = System.nanoTime(); } return v; } @Override public V put(K key, V value) { while (km.size() >= maxSize) { K k = getLFUAging(); km.remove(k); this.remove(k); } V v = super.put(key, value); km.put(key, new HitRate(key, 1, System.nanoTime())); return v; } private K getLFUAging() { HitRate min = Collections.min(km.values()); return min.key; } class HitRate implements Comparable<HitRate> { K key; Integer hitCount; // 命中次數 Long atime; // 上次命中時間 public HitRate(K key, Integer hitCount, Long atime) { this.key = key; this.hitCount = hitCount; this.atime = atime; } @Override public int compareTo(HitRate o) { int hr = hitCount.compareTo(o.hitCount); return hr != 0 ? hr : atime.compareTo(o.atime); } } public static void main(String[] as) throws Exception { LFUAgingMap<String, String> cache = new LFUAgingMap<String, String>(); cache.put("a", "a"); cache.put("b", "b"); cache.put("c", "c"); cache.get("a"); cache.get("a"); cache.put("d", "d"); cache.get("d"); cache.get("c"); cache.put("e", "e"); for (String key : cache.keySet()) { System.out.println(key); } } }