当前位置:首页 > JavaScript

lrucache实现 JS

2026-04-05 05:44:40JavaScript

LRU Cache 实现(JavaScript)

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

lrucache实现 JS

使用 Map 实现(ES6)

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

lrucache实现 JS

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) 时间复杂度实现:

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实现跳表

JS实现跳表

跳表的基本概念 跳表(Skip List)是一种基于概率的数据结构,允许快速查询、插入和删除操作,时间复杂度平均为O(log n)。它通过多层链表实现,每一层都是下一层的子集,高层链表跳过多个元素,从…

JS实现文本的删除

JS实现文本的删除

使用 substring() 方法 通过指定起始和结束索引截取字符串的一部分,间接实现删除效果。 let str = "Hello World"; let newStr = str.substr…

实现阶乘JS

实现阶乘JS

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

JS实现取色

JS实现取色

使用 input[type="color"] 获取颜色值 HTML5 提供了原生颜色选择器,通过 <input type="color"> 可直接调用系统取色工具。用户选择的颜色会以十六进…

JS实现跳表

JS实现跳表

跳表(Skip List)简介 跳表是一种基于概率的平衡数据结构,通过多层链表实现高效查找、插入和删除操作,时间复杂度为 $O(\log n)$。其核心思想是通过“跳跃”式访问节点减少遍历次数。 跳…

JS实现上传

JS实现上传

上传文件的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。以下是一个基础示例: <input ty…