博客
关于我
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/

    你可能感兴趣的文章
    php接口返回数据 用echo 还是return?
    查看>>
    php接口返回状态,大家一般怎么规范接口返回内容
    查看>>
    php接收formdata上传的多个文件,使用formData()上传多个文件
    查看>>
    PHP操作csv文件导入+导出
    查看>>
    php操作mysql用select_php如何操作mysql获取select 结果
    查看>>
    PHP操作符与控制结构
    查看>>
    PHP支付宝SDK使用,电脑网页支付
    查看>>
    php支付宝手机网页支付类实例
    查看>>
    PHP改变数组key值的方法
    查看>>
    php教程之php空白页的原因及解决方法
    查看>>
    PHP数据库操作
    查看>>
    PHP数据文件过大,导致PHP加速器eaccelerator在PHP5.2版本下崩溃
    查看>>
    RabbitMQ - 死信、TTL原理、延迟队列安装和配置
    查看>>
    PHP数据访问的多重查询(租房子查询)
    查看>>
    RabbitMQ - 如保证消息的可靠性?(消息确认、消息持久化、失败重试机制)
    查看>>
    RabbitMQ - 基于 SpringAMQP 带你实现五种消息队列模型
    查看>>
    php数组函数分析--array_column
    查看>>
    php数组去重复数据的小例子
    查看>>
    php数组实现:哈希 +双向链表
    查看>>
    PHP数组排序函数array_multisort()函数详解(二)
    查看>>