基于js的LRU缓存

LRU 介绍 https://zhuanlan.zhihu.com/p/52196637

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) {
      const leastUsedKey = this.cache.keys().next().value;
      this.cache.delete(leastUsedKey);
    }

    this.cache.set(key, value);
  }
}

在构造函数中,我们传入了一个容量参数capacity,并创建了一个名为cache的空Map对象。在get()方法中,我们首先判断cache中是否存在该key,如果不存在则返回-1。如果存在,则将该key对应的值取出,并从cache中删除该key,再重新将该key和其对应的值添加到Map中,以保证其在Map中处于最近使用状态。最后,返回该key对应的值。

put()方法中,我们首先判断cache中是否存在该key,如果是,则删除该key。如果cache已满,则查找cache中最久未使用的key并删除它。然后,将新的(key, value)添加到cache中。

需要注意的是,在上述代码中,我们使用了ES6中的Map数据结构来存储缓存数据,因为它具有按照插入顺序排序的特性,这样可以避免手动维护一个额外的链表来记录缓存数据的访问时间戳等信息。

基于哈希链表的LRU

javascript
//定义哈希链表节点类
class HashNode {
  constructor(key, value) {
    this.key = key;
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

//定义LRU缓存类
class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.map = new Map();
    this.head = new HashNode(null, null);
    this.tail = new HashNode(null, null);
    this.head.next = this.tail;
    this.tail.prev = this.head;
  }

  //获取数据
  get(key) {
    if (this.map.has(key)) {
      const node = this.map.get(key);
      this.moveToHead(node);
      return node.value;
    }
    return -1;
  }

  //插入数据
  put(key, value) {
    if (this.map.has(key)) {
      const node = this.map.get(key);
      node.value = value;
      this.moveToHead(node);
    } else {
      const node = new HashNode(key, value);
      this.map.set(key, node);
      this.addToHead(node);
      if (this.map.size > this.capacity) {
        const removedNode = this.removeFromTail();
        this.map.delete(removedNode.key);
      }
    }
  }

  //将节点插入到链表头部
  addToHead(node) {
    node.prev = this.head;
    node.next = this.head.next;
    this.head.next.prev = node;
    this.head.next = node;
  }

  //将节点移动到链表头部
  moveToHead(node) {
    this.removeNode(node);
    this.addToHead(node);
  }

  //删除节点
  removeNode(node) {
    node.prev.next = node.next;
    node.next.prev = node.prev;
  }

  //从链表尾部删除节点
  removeFromTail() {
    const node = this.tail.prev;
    this.removeNode(node);
    return node;
  }
}

//测试LRU缓存
const lruCache = new LRUCache(2);
lruCache.put(1, 'value1');
lruCache.put(2, 'value2');
console.log(lruCache.get(1)); //输出value1
lruCache.put(3, 'value3');
console.log(lruCache.get(2)); //输出-1
lruCache.put(4, 'value4');
console.log(lruCache.get(1)); //输出-1
console.log(lruCache.get(3)); //输出value3
console.log(lruCache.get(4)); //输出value4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LRU 缓存基于哈希表的实现可以使用 STL 中的 `std::unordered_map`,这个容器提供了 O(1) 的查找和插入操作,非常适合用于实现缓存。下面是一个基于哈希表实现的 LRU 缓存的 C++ 代码: ```cpp #include <unordered_map> #include <list> class LRUCache { public: LRUCache(int capacity) : capacity_(capacity) {} int get(int key) { auto iter = cache_.find(key); if (iter == cache_.end()) { return -1; } else { auto value = iter->second->second; cache_list_.splice(cache_list_.begin(), cache_list_, iter->second); iter->second = cache_list_.begin(); return value; } } void put(int key, int value) { auto iter = cache_.find(key); if (iter == cache_.end()) { if (cache_.size() == capacity_) { int key_to_remove = cache_list_.back().first; cache_.erase(key_to_remove); cache_list_.pop_back(); } cache_list_.push_front({key, value}); cache_[key] = cache_list_.begin(); } else { iter->second->second = value; cache_list_.splice(cache_list_.begin(), cache_list_, iter->second); iter->second = cache_list_.begin(); } } private: int capacity_; std::list<std::pair<int, int>> cache_list_; std::unordered_map<int, std::list<std::pair<int, int>>::iterator> cache_; }; ``` 在这个实现中,我们使用了一个双向链表 `cache_list_` 来保存缓存中的数据,并使用一个哈希表 `cache_` 来提高查找效率。在 `get` 操作中,我们先在哈希表中查找目标数据,如果不存在则返回 -1,否则将目标数据移到链表头部并返回其值。在 `put` 操作中,如果缓存中不存在目标数据则创建一个新的节点并将其添加到链表头部,如果缓存已满则删除链表尾部的节点。如果缓存中已存在目标数据则将其值更新并将其移动到链表头部。 需要注意的是,在 `get` 操作中,我们需要使用 `splice` 操作将目标节点移到链表头部,并更新哈希表中对应节点的指针。在 `put` 操作中,我们需要先判断缓存是否已满,如果已满则删除链表尾部的节点,并在哈希表中删除对应的键值对。然后我们将新节点添加到链表头部,并在哈希表中创建对应的键值对。 另外,由于我们使用了 STL 中的 `std::list`,所以我们可以直接使用 `push_front` 和 `pop_back` 操作来添加和删除链表节点,非常方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值