当前位置:首页 > JavaScript

lrucache实现 JS

2026-04-05 05:44:40JavaScript

LRU Cache 实现(JavaScript)

LRU(Least Recently Used)缓存是一种常见的缓存淘汰策略,当缓存空间不足时,会优先移除最近最少使用的数据。以下是基于 JavaScript 的实现方案:

使用 Map 实现(ES6)

利用 Map 保持键的插入顺序,结合双向链表的思想实现 LRU:

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map();
  }

  get(key) {
    if (!this.cache.has(key)) return -1;
    const value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value); // 更新为最新访问
    return value;
  }

  put(key, value) {
    if (this.cache.has(key)) {
      this.cache.delete(key);
    } else if (this.cache.size >= this.capacity) {
      // 删除最久未使用的键(Map 第一个键)
      const oldestKey = this.cache.keys().next().value;
      this.cache.delete(oldestKey);
    }
    this.cache.set(key, value);
  }
}

双向链表 + 哈希表实现

更高效的 O(1) 时间复杂度实现:

lrucache实现 JS

class ListNode {
  constructor(key, value) {
    this.key = key;
    this.value = value;
    this.prev = null;
    this.next = null;
  }
}

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.size = 0;
    this.hash = {};
    this.head = new ListNode(0, 0);
    this.tail = new ListNode(0, 0);
    this.head.next = this.tail;
    this.tail.prev = this.head;
  }

  _addNode(node) {
    node.prev = this.head;
    node.next = this.head.next;
    this.head.next.prev = node;
    this.head.next = node;
  }

  _removeNode(node) {
    const prev = node.prev;
    const next = node.next;
    prev.next = next;
    next.prev = prev;
  }

  _moveToHead(node) {
    this._removeNode(node);
    this._addNode(node);
  }

  get(key) {
    const node = this.hash[key];
    if (!node) return -1;
    this._moveToHead(node);
    return node.value;
  }

  put(key, value) {
    const node = this.hash[key];
    if (node) {
      node.value = value;
      this._moveToHead(node);
    } else {
      const newNode = new ListNode(key, value);
      this.hash[key] = newNode;
      this._addNode(newNode);
      this.size++;
      if (this.size > this.capacity) {
        const tail = this.tail.prev;
        this._removeNode(tail);
        delete this.hash[tail.key];
        this.size--;
      }
    }
  }
}

使用场景说明

  • Map 实现:适合简单场景,代码简洁但依赖 Map 的插入顺序特性(非标准行为,但主流浏览器支持)。
  • 双向链表实现:标准 O(1) 操作,适合高频访问场景,如前端路由缓存、API 响应缓存等。

注意事项

  1. 缓存容量需根据实际内存限制设置。
  2. 链表实现中需注意节点引用的正确性,避免内存泄漏。
  3. 在 TypeScript 中可添加泛型支持不同类型的键值。

标签: lrucacheJS
分享给朋友:

相关文章

JS如何调用react组件

JS如何调用react组件

调用React组件的常见方法 在JavaScript中调用React组件通常涉及以下几种场景和方式: 直接渲染组件 通过ReactDOM.render()方法将组件渲染到DOM节点: import…

JS能实现跨平台吗

JS能实现跨平台吗

JavaScript 的跨平台能力 JavaScript 是一种高度灵活的脚本语言,通过不同运行时环境和工具链可以实现跨平台开发。以下是其跨平台能力的核心实现方式: 浏览器环境 JavaScript…

用JS实现冰墩墩

用JS实现冰墩墩

以下是用JavaScript绘制冰墩墩的几种实现方法,结合Canvas绘图和SVG路径数据: 使用Canvas绘制基础轮廓 const canvas = document.createEleme…

实现 slice JS

实现 slice JS

实现 JavaScript 的 slice 方法 JavaScript 的 slice 方法用于从数组或字符串中提取一部分,返回一个新的数组或字符串,而不修改原始数据。以下是实现 slice 的核心逻…

实现阶乘JS

实现阶乘JS

递归实现阶乘 递归是一种直接按照数学定义实现阶乘的方法。n的阶乘可以表示为n乘以(n-1)的阶乘,基础情况是0的阶乘为1。 function factorialRecursive(n) { if…

JS实现inpubox

JS实现inpubox

实现 InputBox 的基本结构 使用 HTML 和 CSS 创建一个基础的输入框结构,确保样式简洁且易于扩展。 <div class="input-box"> <input…