1. 设计缓存结构LRU
注:哈希表中,key或value如果存的是简单类型(基本数据类型和String),则哈希表中存的是简单类型的值。如果要往哈希表中添加对象,存的是该对象的地址。
思路:
先设计一个节点Node<key, value>,(key,value的类型可以使用泛型)根据这个节点设计一个双向链表(由头到尾,优先级由低到高),里面存的是Node节点设计双向链表的几个方法:添加节点、删除节点、将某节点移到尾部。
创建一个HashMap<String, Node<key, value>>,可以通过get(i)的方式获取中链表中该节点的地址。如果双向链表出现添加或删除节点操作的话,处理哈希表中对应节点。
import java.util.HashMap;
public class LRU {
//节点类型,有头指针和尾指针
public static class Node<K,V> {
public K key;
public V value;
public Node<K, V> last;
public Node<K, V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
//设计双向链表
public static class NodeDoubleLinkedList<K, V> {
private Node<K, V> head;
private Node<K, V> tail;
public NodeDoubleLinkedList() {
this.head = null;
this.tail = null;
}
public void addNode(Node<K, V> newNode) {
if (newNode == null) {
return;
}
if (this.head == null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.last = this.tail;
this.tail = newNode;
}
}
//每一次操作node节点后,都需要将node节点优先级调到最高
public void moveNodeToTail(Node<K, V> node) {
if (this.tail == node) {
return;
}
if (this.head == node) {
this.head = node.next;
this.head.last = null;
} else {
node.last.next = node.next;
node.next.last = node.last;
}
node.last = this.tail;
node.next = null;
this.tail.next = node;
this.tail = node;
}
public Node<K, V> removeHead() {
if (this.head == null) {
return null;
}
Node<K, V> res = this.head;
if (this.head == this.tail) {
this.head = null;
this.tail = null;
} else {
this.head = res.next;
res.next = null;
this.head.last = null;
}
return res;
}
}
public static class MyCache<K, V> {
private HashMap<K, Node<K, V>> keyNodeMap;
private NodeDoubleLinkedList<K, V> nodeList;
private int capacity;
public MyCache(int capacity) {
if (capacity < 1) {
throw new RuntimeException("should be more than 0.");
}
this.keyNodeMap = new HashMap<K, Node<K, V>>();
this.nodeList = new NodeDoubleLinkedList<K, V>();
this.capacity = capacity;
}
//从内存中取节点
public V get(K key) {
if (this.keyNodeMap.containsKey(key)) {
Node<K, V> res = this.keyNodeMap.get(key);
this.nodeList.moveNodeToTail(res);
return res.value;
}
return null;
}
//往内存中添加节点
public void set(K key, V value) {
if (this.keyNodeMap.containsKey(key)) {
Node<K, V> node = this.keyNodeMap.get(key);
node.value = value;
this.nodeList.moveNodeToTail(node);
} else {
Node<K, V> newNode = new Node<K,V>(key, value);
this.keyNodeMap.put(key, newNode);
this.nodeList.addNode(newNode);
if (this.keyNodeMap.size() == this.capacity + 1) {
this.removeMostUnusedCache();
}
}
}
//容量超标是,删除最久未使用的节点
private void removeMostUnusedCache() {
Node<K, V> removeNode = this.nodeList.removeHead();
K removeKey = removeNode.key;
this.keyNodeMap.remove(removeKey);
}
}
public static void main(String[] args) {
MyCache<String, Integer> testCache = new MyCache<String, Integer>(3);
testCache.set("A", 1);
testCache.set("B", 2);
testCache.set("C", 3);
System.out.println(testCache.get("B"));
System.out.println(testCache.get("A"));
testCache.set("D", 4);
System.out.println(testCache.get("D"));
System.out.println(testCache.get("C"));
}
}
2. 设计可以变更的缓存结构(LFU)
设计思路:对于使用次数相同的节点,放到同一个双向链表里。然后这些双向链表之间相连
2.1 设计节点
public static class Node {
public Integer key;
public Integer value;
public Integer times;
public Node up;//上指针
public Node down;//下指针
public Node(int key, int value, int times) {
this.key = key;
this.value = value;
this.times = times;
}
}
节点的结构如下图
2.2 设计双向链表
public static class NodeList {
public Node head;
public Node tail;
public NodeList last;
public NodeList next;
public NodeList(Node node) {
head = node;
tail = node;
}
...
}
NodeList的结构如下图
2.3 设计缓存结构
public static class LFUCache {
public static class NodeList {
...
}
private int capacity;//最大容量
private int size;//当前总节点个数
private HashMap<Integer, Node> records;//将所有节点保存在哈希表中,key值为节点的key属性
private HashMap<Node, NodeList> heads;//将所有链表存入哈希表中,key值为链表的头
private NodeList headList;//指向第一个链表
public LFUCache(int capacity) {
this.capacity = capacity;
this.size = 0;
this.records = new HashMap<>();
this.heads = new HashMap<>();
headList = null;
}
...
}
如下图:每一个NodeList中,存放的都是出现次数相同的节点。可以选择尾节点优先级最低。如果某NodeList中没有节点了,删除此NodeList
import java.util.HashMap;
public class Code_03_LFU {
public static class Node {
public Integer key;
public Integer value;
public Integer times;
public Node up;
public Node down;
public Node(int key, int value, int times) {
this.key = key;
this.value = value;
this.times = times;
}
}
public static class LFUCache {
public static class NodeList {
public Node head;
public Node tail;
public NodeList last;
public NodeList next;
public NodeList(Node node) {
head = node;
tail = node;
}
public void addNodeFromHead(Node newHead) {
newHead.down = head;
head.up = newHead;
head = newHead;
}
public boolean isEmpty() {
return head == null;
}
public void deleteNode(Node node) {
if (head == tail) {
head = null;
tail = null;
} else {
if (node == head) {
head = node.down;
head.up = null;
} else if (node == tail) {
tail = node.up;
tail.down = null;
} else {
node.up.down = node.down;
node.down.up = node.up;
}
}
node.up = null;
node.down = null;
}
}
private int capacity;
private int size;
private HashMap<Integer, Node> records;
private HashMap<Node, NodeList> heads;
private NodeList headList;
public LFUCache(int capacity) {
this.capacity = capacity;
this.size = 0;
this.records = new HashMap<>();
this.heads = new HashMap<>();
headList = null;
}
public void set(int key, int value) {
if (records.containsKey(key)) {
Node node = records.get(key);
node.value = value;
node.times++;
NodeList curNodeList = heads.get(node);
move(node, curNodeList);
} else {
if (size == capacity) {
Node node = headList.tail;
headList.deleteNode(node);
modifyHeadList(headList);
records.remove(node.key);
heads.remove(node);
size--;
}
Node node = new Node(key, value, 1);
if (headList == null) {
headList = new NodeList(node);
} else {
if (headList.head.times.equals(node.times)) {
headList.addNodeFromHead(node);
} else {
NodeList newList = new NodeList(node);
newList.next = headList;
headList.last = newList;
headList = newList;
}
}
records.put(key, node);
heads.put(node, headList);
size++;
}
}
private void move(Node node, NodeList oldNodeList) {
oldNodeList.deleteNode(node);
NodeList preList = modifyHeadList(oldNodeList) ? oldNodeList.last
: oldNodeList;
NodeList nextList = oldNodeList.next;
if (nextList == null) {
NodeList newList = new NodeList(node);
if (preList != null) {
preList.next = newList;
}
newList.last = preList;
if (headList == null) {
headList = newList;
}
heads.put(node, newList);
} else {
if (nextList.head.times.equals(node.times)) {
nextList.addNodeFromHead(node);
heads.put(node, nextList);
} else {
NodeList newList = new NodeList(node);
if (preList != null) {
preList.next = newList;
}
newList.last = preList;
newList.next = nextList;
nextList.last = newList;
if (headList == nextList) {
headList = newList;
}
heads.put(node, newList);
}
}
}
// return whether delete this head
private boolean modifyHeadList(NodeList nodeList) {
if (nodeList.isEmpty()) {
if (headList == nodeList) {
headList = nodeList.next;
if (headList != null) {
headList.last = null;
}
} else {
nodeList.last.next = nodeList.next;
if (nodeList.next != null) {
nodeList.next.last = nodeList.last;
}
}
return true;
}
return false;
}
public int get(int key) {
if (!records.containsKey(key)) {
return -1;
}
Node node = records.get(key);
node.times++;
NodeList curNodeList = heads.get(node);
move(node, curNodeList);
return node.value;
}
}
}