实现一个LRU缓存机制

设计一个lru机制,首先是思路:

首先用一个双向链表来存储一个节点

type DLinkNode struct{
	key, value int
	pre, next *DLinkNode

}

整个LRU包括当前长度、容量、头节点、尾节点、hash存储

type LRUCache struct {
	size int
	capacity int
	cache map[int]*DLinkNode
	head, tail *DLinkNode
}

数据结构定义好以后,就是操作了,主要包括,初始化一个节点,获取一个节点的值,存储一个极点的值

读操作,首先判断hash中是否有该元素,没有的话,直接返回-1;否则返回该节点值,并将该元素移动至队首,见代码:

func (this *LRUCache) Get(key int) int {
	if _, ok := this.cache[key];!ok{
		return -1
	}
	//移动位置
	node := this.cache[key]
	this.moveToHead(node)
	//返回结果
	return node.value

}

写操作,首先判断hash中是否有该元素,有的话,直接将该元素移动至队首;

否则初始化节点,将该元素插入队首,插入成功后,判断链表长度是否超过容量,不超过,直接返回;超过,删除队尾元素,这里要注意一点,也要删除hash中对应的key值。

完整代码,见图

type LRUCache struct {
	size int
	capacity int
	cache map[int]*DLinkNode
	head, tail *DLinkNode
}

type DLinkNode struct{
	key, value int
	pre, next *DLinkNode

}

func initDLinkNode(key, value int)*DLinkNode{
	return &DLinkNode{
		key:key,
		value:value,
	}
}

func Constructor(capacity int) LRUCache {
	l := LRUCache{
		capacity: capacity,
		cache: map[int]*DLinkNode{},
		head: initDLinkNode(0, 0),
		tail: initDLinkNode(0, 0),
	}
	l.head.next = l.tail
	l.tail.pre = l.head
	return l
}


func (this *LRUCache) Get(key int) int {
	if _, ok := this.cache[key];!ok{
		return -1
	}
	//移动位置
	node := this.cache[key]
	this.moveToHead(node)
	//返回结果
	return node.value

}

func (this *LRUCache)moveToHead(node *DLinkNode){
	this.removeNode(node)
	this.addToHead(node)
}

func (this *LRUCache)removeNode(node *DLinkNode){
	node.pre.next = node.next
	node.next.pre = node.pre
}

func (this *LRUCache)addToHead(node *DLinkNode){
	node.next = this.head.next
	node.pre = this.head
	this.head.next.pre = node
	this.head.next = node
}

func (this *LRUCache) Put(key int, value int)  {
	if _, ok := this.cache[key];ok{
		node := this.cache[key]
		node.key = value
		this.moveToHead(node)
	}else{
		node := initDLinkNode(key, value)
		this.cache[key] = node
		this.addToHead(node)
		this.size++
		if this.size > this.capacity{
			removed := this.removeTail()
			delete(this.cache, removed.key)
			this.size--
		}
	}
}

func (this *LRUCache)removeTail() *DLinkNode{
	node := this.tail.pre
	this.removeNode(node)
	return node
}


/**
 * Your LRUCache object will be instantiated and called as such:
 * obj := Constructor(capacity);
 * param_1 := obj.Get(key);
 * obj.Put(key,value);
 */

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LRU(Least Recently Used)缓存机制是一种常见的缓存淘汰策略,它会将最近最少使用的缓存数据淘汰出去,从而保持缓存的容量在一定的范围内。 在 Java 中,我们可以使用 LinkedHashMap 类来实现 LRU 缓存机制。LinkedHashMap 是一种哈希表和链表实现的有序映射表,它允许我们按照元素的插入顺序或访问顺序进行遍历。我们可以通过设置 accessOrder 参数为 true 来启用按访问顺序排序。 下面是一份简单的 Java 实现代码: ```java import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends LinkedHashMap<K, V> { private int capacity; public LRUCache(int capacity) { super(capacity, 0.75f, true); this.capacity = capacity; } protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > capacity; } } ``` 在上面的代码中,我们继承了 LinkedHashMap 类,并在构造函数中调用了父类的构造函数,同时设置 accessOrder 为 true。我们还实现了 removeEldestEntry 方法,该方法会在每次插入元素时被调用,如果当前缓存的元素数量超过了设定的容量,就会将最老的元素删除。 使用该 LRU 缓存类的示例代码: ```java LRUCache<Integer, Integer> cache = new LRUCache<>(2); cache.put(1, 1); cache.put(2, 2); System.out.println(cache.get(1)); // 输出 1 cache.put(3, 3); System.out.println(cache.get(2)); // 输出 null cache.put(4, 4); System.out.println(cache.get(1)); // 输出 null System.out.println(cache.get(3)); // 输出 3 System.out.println(cache.get(4)); // 输出 4 ``` 在上面的示例代码中,我们创建了一个容量为 2 的 LRU 缓存,并且按顺序插入了三个元素。由于容量只有 2,因此最后插入的元素 4 会将元素 1 淘汰出缓存,而元素 2 虽然是最近使用的,但是由于已经被淘汰出缓存,因此返回的值为 null。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值