show me the code - LFU/LRU Java
LFU
/**
* Created by kangkaisen on 16/5/4.
* 关键点:
* 1. 通过一个hashmap记录每个key访问情况
* 2. key访问情况可用用HitRate
* 3. HitRate中除了记录访问次数,还要key对象,用于反向索引查询key,清理hashmap
* 4. HitRate中lastTime用于访问次数相同时的比较
*/
import java.util.*;
public class LFUCache {
private static final int DEFAULT_MAX_SIZE = 3;
private int capacity = DEFAULT_MAX_SIZE;
//保存缓存的访问频率和时间
private final Map<Integer, HitRate> cache = new HashMap<Integer, HitRate>();
//保存缓存的KV
private final Map<Integer, Integer> KV = new HashMap<Integer, Integer>();
// @param capacity, an integer
public LFUCache(int capacity) {
this.capacity = capacity;
}
// @param key, an integer
// @param value, an integer
// @return nothing
public void set(int key, int value) {
Integer v = KV.get(key);
if (v == null) {
if (cache.size() == capacity) {
Integer k = getKickedKey();
KV.remove(k);
cache.remove(k);
}
cache.put(key, new HitRate(key, 1, System.nanoTime()));
} else { //若是key相同只增加频率,更新时间,并不进行置换
HitRate hitRate = cache.get(key);
hitRate.hitCount += 1;
hitRate.lastTime = System.nanoTime();
}
KV.put(key, value);
}
public int get(int key) {
Integer v = KV.get(key);
if (v != null) {
HitRate hitRate = cache.get(key);
hitRate.hitCount += 1;
hitRate.lastTime = System.nanoTime();
return v;
}
return -1;
}
// @return 要被置换的key
private Integer getKickedKey() {
HitRate min = Collections.min(cache.values());
return min.key;
}
class HitRate implements Comparable<HitRate> {
Integer key;
Integer hitCount; // 命中次数
Long lastTime; // 上次命中时间
public HitRate(Integer key, Integer hitCount, Long lastTime) {
this.key = key;
this.hitCount = hitCount;
this.lastTime = lastTime;
}
public int compareTo(HitRate o) {
int hr = hitCount.compareTo(o.hitCount);
return hr != 0 ? hr : lastTime.compareTo(o.lastTime);
}
}
public static void main(String[] as) throws Exception {
LFUCache cache = new LFUCache(3);
cache.set(2, 2);
cache.set(1, 1);
System.out.println(cache.get(2));
System.out.println(cache.get(1));
System.out.println(cache.get(2));
cache.set(3, 3);
cache.set(4, 4);
System.out.println(cache.get(3));
System.out.println(cache.get(2));
System.out.println(cache.get(1));
System.out.println(cache.get(4));
}
}
LRU
import java.util.HashMap;
/**
* Created by kangkaisen on 16/5/5.
*/
//实现起来比较简单. 维护一个链表,每次数据新添加或者有访问时移动到链表尾,
//每次淘汰数据则是淘汰链表头部的数据.
//也就是最近最少访问的数据在链表头部,最近刚访问的数据在链表尾部
public class LRUCache {
private class Node{
Node prev;
Node next;
int key;
int value;
public Node(int key, int value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}
private int capacity;
private HashMap<Integer, Node> hs = new HashMap<Integer, Node>();
private Node head = new Node(-1, -1);
private Node tail = new Node(-1, -1);
// @param capacity, an integer
public LRUCache(int capacity) {
// write your code here
this.capacity = capacity;
tail.prev = head;
head.next = tail;
}
// @return an integer
public int get(int key) {
// write your code here
if( !hs.containsKey(key)) {
return -1;
}
// remove current
Node current = hs.get(key);
current.prev.next = current.next;
current.next.prev = current.prev;
// move current to tail
move_to_tail(current);
return hs.get(key).value;
}
// @param key, an integer
// @param value, an integer
// @return nothing
public void set(int key, int value) {
// write your code here
if( get(key) != -1) {
hs.get(key).value = value;
return;
}
if (hs.size() == capacity) {
hs.remove(head.next.key);
head.next = head.next.next;
head.next.prev = head;
}
Node insert = new Node(key, value);
hs.put(key, insert);
move_to_tail(insert);
}
private void move_to_tail(Node current) {
current.prev = tail.prev;
tail.prev = current;
current.prev.next = current;
current.next = tail;
}
public static void main(String[] as) throws Exception {
LRUCache cache = new LRUCache(3);
cache.set(2, 2);
cache.set(1, 1);
System.out.println(cache.get(2));
System.out.println(cache.get(1));
System.out.println(cache.get(2));
cache.set(3, 3);
cache.set(4, 4);
System.out.println(cache.get(3));
System.out.println(cache.get(2));
System.out.println(cache.get(1));
System.out.println(cache.get(4));
}
}