博客
关于我
LRU的map+双链表实现(Go描述)
阅读量:749 次
发布时间:2019-03-22

本文共 2669 字,大约阅读时间需要 8 分钟。

基于Map和双链表实现的LRU缓存算法

作者:lzw5399

日期:2021/5/20 22:28
描述:本文将介绍基于Map和双链表实现的LRU(最近使用)缓存算法的具体实现方式,详细阐述Set和Get操作的实现逻辑。

代码示例:

public class LRUCache {    private int capacity;    private Map
cache; private LinkedNode head; private LinkedNode tail; private ReentrantLock lock; public LRUCache(int capacity) { this.capacity = capacity; this.cache = new HashMap<>(); this.head = null; this.tail = null; this.lock = new ReentrantLock(); } public void set(int key, int value) { LinkedNode node = this.cache.get(key); if (node != null) { moveToFront(node); return; } if (capacity == cache.size()) { LinkedNode removed = removeTail(); this.cache.remove(removed.getKey()); } LinkedNode newNode = new LinkedNode(); newNode.key = key; newNode.value = value; addToFront(newNode); this.cache.put(key, newNode); } public int get(int key) { LinkedNode node = this.cache.get(key); if (node == null) { return -1; } moveToFront(node); return node.value; } private void moveToFront(LinkedNode node) { removeNode(node); addFront(node); } private LinkedNode removeTail() { if (tail.prev == null) { return tail; } LinkedNode removed = tail.prev; tail.prev.next = null; tail.prev = null; return removed; } private LinkedNode removeNode(LinkedNode node) { if (node.prev != null) { node.prev.next = node.next; } if (node.next != null) { node.next.prev = node.prev; } if (node.prev == null) { head = node.next; } if (node.next == null) { tail = node.prev; } return node; } private void addFront(LinkedNode node) { if (head == null) { head = tail = node; } else { node.prev = null; head.prev = node; } head = node; }}

代码说明:

  • 结构设计

    • LRUCache 类实现了一个基于Map和双链表的缓存机制,能够根据Least Recently Used(LRU)算法自动管理缓存空间。
    • 主要字段:
      • capacity:缓存的最大容量。
      • cache:存储缓存键值对的Map,同时每个键值对对应一个双链表节点。
      • headtail:双链表的头尾节点。
      • lock:用于保证多线程环境下的数据同步。
  • Set操作实现

    • set 方法用于将键值对加入缓存:
      • 检查是否存在相同的键,如果存在则将该节点移动到链表头部。
      • 如果不存在,检查缓存是否已满:
        • 满则移除链表尾部的节点,并删除Map中的对应记录。
        • 新增节点,插入链表头部,并将键值对加入Map。
  • Get操作实现

    • get 方法用于从缓存中获取指定键的值:
      • 检查是否存在对应的键,不存在则返回-1。
      • 存在则将该节点移动到链表头部,并返回对应的值。
  • 链表操作

    • moveToFront:将指定节点从当前位置移动到链表头部,需要先移除原有的位置,再插入头部。
    • removeTail:移除链表尾部的节点。
    • removeNode:移除指定节点,调整链表头部和尾部指针。
    • addFront:将节点插入链表头部,调整头部指针。
  • 这种设计保证了Set和Get操作的时间复杂度均为O(1),能够高效管理缓存。

    转载地址:http://qnkwk.baihongyu.com/

    你可能感兴趣的文章
    POJ 2486 树形dp
    查看>>
    POJ 2488:A Knight&#39;s Journey
    查看>>
    SpringBoot为什么易学难精?
    查看>>
    poj 2545 Hamming Problem
    查看>>
    poj 2723
    查看>>
    poj 2763 Housewife Wind
    查看>>
    Qt笔记——模型/视图MVD 文件目录浏览器软件
    查看>>
    POJ 2892 Tunnel Warfare(树状数组+二分)
    查看>>
    poj 2965 The Pilots Brothers' refrigerator-1
    查看>>
    poj 3026( Borg Maze BFS + Prim)
    查看>>
    POJ 3041 - 最大二分匹配
    查看>>
    POJ 3041 Asteroids(二分匹配模板题)
    查看>>
    Qt笔记——标准文件对话框QFileDialog
    查看>>
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>
    poj 3264(简单线段树)
    查看>>
    Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
    查看>>