`

LRU Cache

阅读更多
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

LRU(Least Recently Used 最近最少使用)是内存管理的一种页面置换算法,对于在内存中但又不用的数据块(内存块)叫做LRU,操作系统会根据哪些数据属于LRU而将其移出内存而腾出空间来加载另外的数据。这里采用双向链表加HashMap来实现,cache用双向链表连起来,最近访问的都放在链表的头部,经过几次访问尾部的就是不经常访问到的,如果cache已满,就把链表尾部的记录删除。代码如下:
public class LRUCache {
    private int cacheSize;
    private CacheNode first = new CacheNode();
    private CacheNode last = new CacheNode();
    private HashMap<Integer, CacheNode> cache;
    
    public LRUCache(int capacity) {
        cacheSize = capacity;
        first.prev = last;
        last.next = first;
        cache = new HashMap<Integer, CacheNode>(capacity);
    }
    
    public int get(int key) {
        CacheNode node = cache.get(key);
        if(node != null) {
            moveToHead(node);
            return node.value;
        } else {
            return -1;
        }
    }
    
    public void set(int key, int value) {
        CacheNode node = null;
        if(!cache.containsKey(key)) {
            if(cache.size() == cacheSize) removeTail();
            node = new CacheNode(); 
            node.key = key;
            node.value = value;
            cache.put(key, node);
        } else {
            node = cache.get(key); 
            node.value = value;
        }
        moveToHead(node);
    }
    
    private void moveToHead(CacheNode node) {
        if(node.prev != null) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
        }
        CacheNode head = first.prev;
        first.prev = node;
        node.next = first;
        node.prev = head;
        head.next = node;
    }
    
    private void removeTail() {
        CacheNode lastNode = last.next;
        last.next = last.next.next;
        last.next.prev = last;
        cache.remove(lastNode.key);
    }
}

class CacheNode {
    CacheNode prev;
    CacheNode next;
    int value;
    int key;
    
}
分享到:
评论

相关推荐

    算法面试通关40讲完整课件 57-58 LRU Cache

    算法面试通关40讲完整课件 57-58 LRU Cache 算法面试通关40讲完整课件 57-58 LRU Cache 算法面试通关40讲完整课件 57-58 LRU Cache 算法面试通关40讲完整课件 57-58 LRU Cache 算法面试通关40讲完整课件 57-58 LRU ...

    最近最少使用cache,提取leveldb的lru cache部分,增加过期时间,过期失效

    提取google开源软件leveldb的lru cache部分,独立出来,并增加过期时间,过期失效,可独立使用。仅供参考。

    Android性能优化一 : 1.The Magic of LRU Cache (100 Days of Google Dev)

    谷歌官方视频

    LRU Cache的简单C++实现

     LRU Cache是一个Cache的置换算法,含义是“近少使用”,把满足“近少使用”的数据从Cache中剔除出去,并且保证Cache中第一个数据是近刚刚访问的,因为这样的数据更有可能被接下来的程序所访问。  LRU的应用比较...

    LRU-update-Cache-.zip_LRU cache

    LRU置换算法是选择最近最久未使用的页面予以置换。该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来经历的时间T,当须淘汰一个页面时,选择现有页面中T值最大的,即最近最久没有访问的页面。这是...

    Lru Cache java代码

    LruCache的java代码实现,是从android代码中抽取出来的

    erlang lru cache module

    NULL 博文链接:https://erlangdisplay.iteye.com/blog/315483

    ARM高速缓存(Cache)Verilog代码 包含ISE工程

    Cache替换策略: LRU I_Cache的工作就是在cpu需要指令时将指令从主存中搬进I_Cache,再传给CPU,而D_Cache在解决数据读外,还要注意数据写入的问题。本工程可以与arm.v 中的arm 核协同工作,主存使用dram_ctrl_sim。

    lru-cache-js:使用双链表和映射实现 LRU Cache,读写时间复杂度为 O(1)

    LRU缓存使用的数据结构使用双向链表实现的队列。 队列的最大大小将等于...执行 npm i lru-cache-js-map const LRUCache = require('lru-cache-js-map');var cache = new LRUCache(100);for(var i=0; i &lt; 100; i++){

    Python实现的一个简单LRU cache

    python的dict 是一个非常简单的cache,但是由于数据量很大,内存很可能增长的过大,因此需要限定记录数,并用LRU算法丢弃旧记录。key 是整型,value是10KB左右的python对象 分析: 1)可以想到,在对于cache,我们...

    LRU更新Cache

    是计算机体系结构试验程序,用LRU算法更新Cache

    javalruleetcode-LRU_cache:LRU_cache

    LRU_cache (Leetcode 146) 设计和实现最近最少使用 (LRU) 缓存的数据结构。 它应该支持以下操作:get 和 set。 get(key) – 如果键存在于缓存中,则获取键的值(将始终为正),否则返回 -1。 set(key, value) – ...

    javalruleetcode-LRUCache:LeetCode问题146Java中的LRU缓存

    Cache in Java 我很喜欢这个问题。 我在康奈尔大学的第一个 CS 课程涉及队列和其他数据结构。 对于其中一个课堂项目,我们必须开发基于队列的模拟。 在工作中,我开发了广泛的队列库。 我能说什么; 我喜欢队列和...

    lru_cache_demo

    如果要使用图片Cache, Android官网和都建议使用LRU Cache,他可以限定cache的大小并且把最少使用的cache清除掉。 所以我们用ListView呈现从网路上抓下来的图片 private String [] mImgsPath = { " ...

    lrucacheleetcode-LRU_Cache_Algorithm:用C++实现LRU缓存算法

    lru cache leetcode LRU Cache Algorithm (LRU缓存算法) C++版本,适用于 ##LRU算法介绍 引自: Discards the least recently used items first. This algorithm requires keeping track of what was used when, ...

    backports.functools_lru_cache

    在发布的Python 3.3中的functools.lru_cache的反向。 用法 考虑使用此技术导入“ lru_cache”函数: try: from functools import lru_cache except ImportError: from backports.functools_lru_cache import lru_...

    LRU Cache-开源

    该项目将利用LRU(最近使用)算法开发内存中对象缓存。 缓存将在固定大小的内存池中存储最常访问的对象。 因此,系统保持统计并计算折衷。

    lru-cache:C ++中功能完整的LRU缓存实现

    lru-cache:C ++中功能完整的LRU缓存实现

    java 缓存 cache lru 实例

    java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例 java 缓存 cache lru 实例

    node-lru:nodejs实现的lru缓存

    Node LRU Cache 基于Nodejs开发的LRU Cache, 兼有缓存超时清除功能 usage var options = { expires: 5 * 60 * 1000, capacity: 5 }; var LRU = require('node-lru'); var cache = new LRU(2);//var cache = new ...

Global site tag (gtag.js) - Google Analytics