博客
关于我
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,同时每个键值对对应一个双链表节点。
      • head 和 tail:双链表的头尾节点。
      • lock:用于保证多线程环境下的数据同步。
  • Set操作实现

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

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

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

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

    你可能感兴趣的文章
    Python:修改 PATH 环境变量的平台无关方式
    查看>>
    Python:假与 0
    查看>>
    Python:函数使用
    查看>>
    Python:函数进阶
    查看>>
    Python:在整数的二进制表示中查找最长的二进制间隙
    查看>>
    Python:如何从二进制转换为 base 64 并返回?
    查看>>
    Python:如何以非阻塞方式读取子进程的标准输出
    查看>>
    Python:如何使用 plotly 制作阴影区域或交替背景颜色?
    查看>>
    Python:如何根据日期时间获取值的计数
    查看>>
    python:如何检测串行 COM 上的设备名称/ID
    查看>>
    Python:如何模拟 datetime.utcnow()?
    查看>>
    Python:如果DataFrames之间的其他值匹配,则对DataFrame中的值求和
    查看>>
    Python:字典思维
    查看>>
    Python:将 HEX 字符串转换为字节
    查看>>
    Python:将列表与范围列表合并
    查看>>
    Python:将列表列表初始化为一定大小,
    查看>>
    Python:带有生成器的给定集合的幂集
    查看>>
    Python:按字符的位置拆分字符串
    查看>>
    Python:替换嵌套字典中的值
    查看>>
    Python:爬取数据出现response.status_code为403解决方法
    查看>>