# LRU Cache

Hard

## Question

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: `get` and `set`.

`get(key)` - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

`set(key, value)` - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

## Solution

#### *(Preferred Implementation) Use Doubly Linked List

``````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> hm = 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) {
this.capacity = capacity;
}

// @return an integer
public int get(int key) {
if (!hm.containsKey(key)) {
return -1;
}
Node current = hm.get(key);
current.prev.next = current.next;
current.next.prev = current.prev;

moveToTail(current);

return hm.get(key).value;
}

// @param key, an integer
// @param value, an integer
// @return nothing
public void set(int key, int value) {
if (get(key) != -1) {
hm.get(key).value = value;
return;
}
if (hm.size() == capacity) {
}
Node insert = new Node(key, value);
hm.put(key, insert);
moveToTail(insert);
}

private void moveToTail(Node current) {
current.next = tail;
tail.prev.next = current;
current.prev = tail.prev;
tail.prev = current;
}
}
``````

``````class Node{
int key;
int value;
Node pre;
Node next;

public Node(int key, int value){
this.key = key;
this.value = value;
}
}

public class LRUCache {
int capacity;
HashMap<Integer, Node> map = new HashMap<Integer, Node>();
Node end=null;

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {
if(map.containsKey(key)){
Node n = map.get(key);
remove(n);
return n.value;
}

return -1;
}

public void remove(Node n){
if(n.pre!=null){
n.pre.next = n.next;
}else{
}

if(n.next!=null){
n.next.pre = n.pre;
}else{
end = n.pre;
}

}

n.pre = null;

if(end ==null)
}

public void set(int key, int value) {
if(map.containsKey(key)){
Node old = map.get(key);
old.value = value;
remove(old);
}else{
Node created = new Node(key, value);
if(map.size()>=capacity){
map.remove(end.key);
remove(end);

}else{
}

map.put(key, created);
}
}
}
``````

#### 中文注释版：https://www.jianshu.com/p/ee6343126728

``````public class LRUCache {
//“潜水”链表节点，抽象
static class Node{
//键值对
private int key;
private int value;

//维护“潜水”键值对，双向链表
private Node pre;
private Node next;

//构造器
Node(){}

Node(int key,int value){
this.key = key;
this.value = value;
}
}

//指定的容量
private int cap;

//保留“潜水”双向链表的头尾指针
private Node tail;

//保存键值对的map
private HashMap<Integer,Node> map;

//构造器参数是：指定的容量
public LRUCache(int capacity) {
this.cap = capacity;

//初始化头尾节点，这里的头结点是辅助节点

//构造器初试容量这样设置可以保证map
//不会发生扩容，详见之前的HashMap
//讲解文章
map = new HashMap<>((int)(cap/0.75)+1);
}

//将指定节点从链表中删除
private void removeNode(Node cur){
if(cur==tail){
tail = tail.pre;

tail.next = null;
cur.pre = null;
}else{
cur.pre.next = cur.next;
cur.next.pre = cur.pre;

cur.pre = null;
cur.next = null;
}
}

//将指定节点追加到链表末尾
tail.next = cur;
cur.pre = tail;

tail = cur;
}

//访问一个键值对
public int get(int key) {
Node cur = map.get(key);
//不存在这个key
if(cur==null){
return -1;
}else{//存在
//含义是当前潜水节点已经被访问了
//将这个节点添加到链表末尾
removeNode(cur);

return cur.value;
}
}

//存储一个键值对
public void put(int key, int value) {
Node cur =  map.get(key);

if(cur==null){
//put前不存在这个key
cur = new Node(key,value);

//将该键值对移动到链表末尾
map.put(key,cur);

//超出了容量，移除链表头结点
//后面那个元素(头结点是辅助节点)
removeNode(outDate);

//不能忘记这里
map.remove(outDate.key);
}
}else{

//put之前已经存在
//将这个键值对移到链表末尾即可
removeNode(cur);
//更新这个key的值
cur.value = value;
}
}

}
``````

``````import java.util.LinkedHashMap;

public class LRUCache {

private Map<Integer, Integer> map;
private final int maxEntries;

public LRUCache(int capacity) {
this.maxEntries = capacity;
map = new LinkedHashMap<Integer, Integer>(16, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > capacity;
}
};
}

public int get(int key) {
return map.getOrDefault(key, -1);
}

public void set(int key, int value) {
map.put(key,value);
}
}
``````
``````import java.util.LinkedHashMap;
public class LRUCache {
private final int CAPACITY;
public LRUCache(int capacity) {
CAPACITY = capacity;
map = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true){
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > CAPACITY;
}
};
}
public int get(int key) {
return map.getOrDefault(key, -1);
}
public void set(int key, int value) {
map.put(key, value);
}
}
``````

``````"true for access-order, false for insertion-order"
``````

### Follow-up for LRU Cache

Concurrent LRU cache implementation

See stackoverflow:

https://stackoverflow.com/questions/40239485/concurrent-lru-cache-implementation

If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the

`Collections.synchronizedMap`

method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

The best you can do is to make it thread-safe is to wrap it with `Collections.synchronizedMap(map)` as explained in the JavaDoc:

``````Map m = Collections.synchronizedMap(new LinkedHashMap(...));
``````

However it is not enough to make it fully thread-safe you sill need to protect any iteration over the content of the map using the instance of the wrapped map as object's monitor:

``````Map m = Collections.synchronizedMap(map);
...
Set s = m.keySet();  // Needn't be in synchronized block
...
synchronized (m) {  // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
``````