厉害了,自己动手实现 LRU 缓存机制!
Java技术栈
共 7928字,需浏览 16分钟
·
2021-05-15 11:22
点击关注公众号,Java干货及时送达
前言
最近在逛博客的时候看到了有关Redis方面的面试题,其中提到了Redis在内存达到最大限制的时候会使用LRU等淘汰机制,然后找了这方面的一些资料与大家分享一下。
第一种实现(使用LinkedHashMap)
public class LRUCache {
int capacity;
Map<Integer,Integer> map;
public LRUCache(int capacity){
this.capacity = capacity;
map = new LinkedHashMap<>();
}
public int get(int key){
//如果没有找到
if (!map.containsKey(key)){
return -1;
}
//找到了就刷新数据
Integer value = map.remove(key);
map.put(key,value);
return value;
}
public void put(int key,int value){
if (map.containsKey(key)){
map.remove(key);
map.put(key,value);
return;
}
map.put(key,value);
//超出capacity,删除最久没用的即第一个,或者可以复写removeEldestEntry方法
if (map.size() > capacity){
map.remove(map.entrySet().iterator().next().getKey());
}
}
public static void main(String[] args) {
LRUCache lruCache = new LRUCache(10);
for (int i = 0; i < 10; i++) {
lruCache.map.put(i,i);
System.out.println(lruCache.map.size());
}
System.out.println(lruCache.map);
lruCache.put(10,200);
System.out.println(lruCache.map);
}
![](https://filescdn.proginn.com/870815f71d07e31fdc82fa0685f2490f/33f00450cca55f2b78b04103a0fb02ef.webp)
第二种实现(双链表+hashmap)
public class LRUCache {
private int capacity;
private Map<Integer,ListNode>map;
private ListNode head;
private ListNode tail;
public LRUCache2(int capacity){
this.capacity = capacity;
map = new HashMap<>();
head = new ListNode(-1,-1);
tail = new ListNode(-1,-1);
head.next = tail;
tail.pre = head;
}
public int get(int key){
if (!map.containsKey(key)){
return -1;
}
ListNode node = map.get(key);
node.pre.next = node.next;
node.next.pre = node.pre;
return node.val;
}
public void put(int key,int value){
if (get(key)!=-1){
map.get(key).val = value;
return;
}
ListNode node = new ListNode(key,value);
map.put(key,node);
moveToTail(node);
if (map.size() > capacity){
map.remove(head.next.key);
head.next = head.next.next;
head.next.pre = head;
}
}
//把节点移动到尾巴
private void moveToTail(ListNode node) {
node.pre = tail.pre;
tail.pre = node;
node.pre.next = node;
node.next = tail;
}
//定义双向链表节点
private class ListNode{
int key;
int val;
ListNode pre;
ListNode next;
//初始化双向链表
public ListNode(int key,int val){
this.key = key;
this.val = val;
pre = null;
next = null;
}
}
}
像第一种方式,如果复写removeEldestEntry会更简单,这里简单的展示一下
public class LRUCache extends LinkedHashMap<Integer,Integer> {
private int capacity;
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
}
另外,关注公众号Java技术栈,在后台回复:面试,可以获取我整理的 Redis 系列面试题和答案,非常齐全。
原文链接:https://blog.csdn.net/Grady00/article/details/115168822
版权声明:本文为CSDN博主「拉霍拉卡」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
![](https://filescdn.proginn.com/8f00f43e8487613615444c4962f556ad/7c04bda134615170352a44a75f8a7173.webp)
![](https://filescdn.proginn.com/f7d6bdc452d4800d413c04e5a51b9c9c/cc5a9c8ba292eac34f4a385869743a4d.webp)
![](https://filescdn.proginn.com/526ff43cd40d4edfbed8db0995e8b022/f4ed80dda9a36777f5c3281c59fff647.webp)
![](https://filescdn.proginn.com/26ff6ba1f006b02003b98da30bedac00/af86870a0e987274b2acc932f2758dc3.webp)
![](https://filescdn.proginn.com/f2a50c78f110a116ef666854f46847cd/1d976c7a0388cc4d2a65d30c3594fb47.webp)
关注Java技术栈看更多干货
![](https://filescdn.proginn.com/1cb84956adec57da062cad738742cce6/8744cbf2cbfffee669dd910ab4e3c8d6.webp)
评论