LRU缓存策略
来源:互联网 发布:苹果手机搞怪拍照软件 编辑:程序博客网 时间:2024/04/30 08:18
为最近最少使用(LRU)缓存策略设计一个数据结构,它应该支持以下操作:获取数据(get)和写入数据(set)。
获取数据get(key):如果缓存中存在key,则获取其数据值(通常是正数),否则返回-1。
写入数据set(key, value):如果key还没有在缓存中,则写入其数据值。当缓存达到上限,它应该在写入新数据之前删除最近最少使用的数据用来腾出空闲位置。
用一个链表来表示全部的缓存数据,显然只需要将最近的访问过的结点移到队尾,那么头部自然就是没有访问过的结点。因为需要经常移动结点,从时间上来考虑使用双向链表是很好的选择.
节点中包含键值对,hashmap存储key和val,通过在map中查找key来定位结点是比较方便的做法
总结:用双向链表来表示结点关系,用map来存储键值对.
public class Solution { public class Node{ Node pre; Node next; int key; int val; public Node(int key, int val){ this.key = key; this.val = val; this.pre = null; this.next = null; } } public int capacity; public HashMap<Integer, Node> map = new HashMap<Integer, Node>(); public Node head = new Node(-1,-1); public Node tail = new Node(-1,-1); // @param capacity, an integer public Solution(int capacity) { this.capacity = capacity; head.next = tail; tail.pre = head; } // @return an integer public int get(int key) { if(!map.containsKey(key)){ //不存在该结点 return -1; } Node current = map.get(key); current.next.pre = current.pre; //最近访问过的结点放到队尾 current.pre.next = current.next; move_to_tail(current); return map.get(key).val; } // @param key, an integer // @param value, an integer // @return nothing public void set(int key, int value) { if(get(key) != -1){ //缓存中已经包含结点 Node node = map.get(key); //取下结点 node.pre.next = node.next; node.next.pre = node.pre; move_to_tail(node); //移到队尾 map.get(key).val = value; //新的值 return; } if(map.size() == capacity){ //缓存已满 map.remove(head.next.key); head.next = head.next.next; //移除头 head.next.pre = head; } Node insert = new Node(key,value); move_to_tail(insert); map.put(key,insert); //添加新的结点 } //移动结点到队尾 public void move_to_tail(Node node){ node.pre = tail.pre; tail.pre.next = node; node.next = tail; tail.pre = node; }}
0 0
- LRU 缓存策略
- LRU 缓存策略
- LintCode-LRU缓存策略
- LRU缓存策略
- LRU缓存策略
- lintcode,LRU缓存策略
- LRU缓存策略
- LRU缓存策略设计
- LRU缓存策略设计
- LRU缓存策略-LintCode
- LRU缓存策略
- lintcode LRU缓存策略
- 【链表】实现LRU缓存策略LRU Cache
- 缓存失效策略(fifo lru lfu)
- LRU策略
- 利用LinkedHashMap简单实现基于LRU策略的缓存
- 简单LRU算法实现缓存大小的限制策略
- FIFO、LFU、LRU三种缓存策略之间的区别
- wed的发布
- 1
- 表单边框的应用
- 内存分配器memblock
- 计算机网络——物理层和信道复用(频分、时分、码分)技术
- LRU缓存策略
- 表单页面设计
- 1038: 算法提高 分苹果
- java初始化和清除
- 表单结合列表项目
- CJOJ P1619 - 【中学高级本】地毯填补
- 超链接的使用
- 03-用私有构造器或者枚举类型强化 Singleton 属性
- Tarjan算法