首页 > 其他分享 >LRU Cache

LRU Cache

时间:2023-04-02 14:33:05浏览次数:26  
标签:Node int Cache next current LRU key prev

Problem Statement

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.

题解

Java

public class Solution {
    private int capacity;
    private HashMap<Integer, Node> map = new HashMap<>();
    private Node head = new Node(-1, -1), tail = new Node(-1, -1);

    private class Node {
        Node prev, next;
        int val, key;

        public Node(int key, int val) {
            this.val = val;
            this.key = key;
            prev = null;
            next = null;
        }
    }

    public Solution(int capacity) {
        this.capacity = capacity;
        tail.prev = head;
        head.next = tail;
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        // remove current
        Node currentNode = map.get(key);
        currentNode.prev.next = currentNode.next;
        currentNode.next.prev = currentNode.prev;

        // move current to tail;
        moveToTail(currentNode);

        return currentNode.val;
    }

    public void set(int key, int value) {
        if (get(key) != -1) {
            map.get(key).val = value;
            return;
        }
        if (map.size() == capacity) {
            map.remove(head.next.key);
            head.next = head.next.next;
            head.next.prev = head;
        }
        Node insert = new Node(key, value);
        map.put(key, insert);
        moveToTail(insert);
    }

    private void moveToTail(Node current) {
        current.prev = tail.prev;
        tail.prev = current;
        current.prev.next = current;
        current.next = tail;
    }
}

 

标签:Node,int,Cache,next,current,LRU,key,prev
From: https://www.cnblogs.com/lyc94620/p/17280420.html

相关文章

  • Redis基于@Cacheable注解实现接口缓存
    说明@Cacheable注解在方法上,表示该方法的返回结果是可以缓存的。也就是说,该方法的返回结果会放在缓存中,以便于以后使用相同的参数调用该方法时,会返回缓存中的值,而不会实际执行该方法。属性名称属性描述举例value/cacheNames指定缓存组件的名字@Cacheable(value="......
  • [ABC273D] LRUD Instructions
    题目链接题解模拟题。观察题目,我们发现,无论问的是前/后/左/右,你都只会在一条直线上走,那对于这条直线,我们可以记录所有这条直线上的障碍物,然后找到距离当前点最近的障碍物,也就是说我们只能走到那个障碍物那块。虽然数据范围高达\(10^9\),但是\(n\le10^5\),所以用\(map\)套\(......
  • FastCGI server uwsgi server SCGI server memcached server
     NGINXReverseProxy|NGINXPlushttps://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/fastcgi_pass passesarequesttoaFastCGIserveruwsg......
  • memcached的slab钙化问题
    memcached的slab钙化问题现象:服务某些接口成功率波动,经研发查看是数据在memcached查询的偶尔失败。因为前一晚升级的时候,改了存储在memcahed的数据(图片base64)的大小,改成......
  • 【坚持每日一题9.25】LRU 缓存
    设计和构建一个“最近最少使用”缓存,该缓存会删除最近最少使用的项目。缓存应该从键映射到值(允许你插入和检索特定键对应的值),并在初始化时指定最大容量。当缓存被填满时,它......
  • LRUCache具体使用
    LRUCache具体使用LRUCache是一种常见的缓存策略,通过最近最少使用的原则,在缓存满时考虑淘汰最近没有使用的数据。可以在Android中作为一个内存缓存工具使用,比如用于加载图......
  • Spring 3.2.1.RELEASE MVC 基于注解ehcache.xml 配置方式
    载的关联包里的ehcache-spring-annotations.jar之外,还需要spring-context-support.jar,cblib-2.2.jar.<dependency><groupId>com.googlecod......
  • redis-SpringCache-简介
    整合&体验@Cacheable引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>......
  • pyhton中_自动生成的_pycache__文件夹
    _pycache__文件夹可以看作该文件夹下文件已被python接管或者说编译过。在第一次执行代码的时候,Python解释器已经把编译的字节码放在__pycache__文件夹中,这样以后再次运行......
  • 本地缓存 GuavaCache & Caffeine
    1.GuavaCacheGuavaCache是一個全内存的本地缓存实现,提供了线程安全实现机制1.1GuavaCache数据结构底层类似ConcurrentlHashMap,所以是线程安全的(分段锁)  1.2Gu......