ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

278,LRU缓存机制

2021-06-14 22:51:47  阅读:191  来源: 互联网

标签:node 缓存 get int cache DLinkedNode LRU key 278


运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制。它应该支持以下操作:获取数据 get 和 写入数据 put 。

 

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。

写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

示例:

LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);

cache.put(2, 2);

cache.get(1);       // 返回  1

cache.put(3, 3);    // 该操作会使得密钥 2 作废

cache.get(2);       // 返回 -1 (未找到)

cache.put(4, 4);    // 该操作会使得密钥 1 作废

cache.get(1);       // 返回 -1 (未找到)

cache.get(3);       // 返回  3

cache.get(4);       // 返回  4

答案:

 1class LRUCache1 {
2    class DLinkedNode {
3        int key;
4        int value;
5        DLinkedNode pre;
6        DLinkedNode post;
7    }
8
9    private void addNode(DLinkedNode node) {
10        node.pre = head;
11        node.post = head.post;
12        head.post.pre = node;
13        head.post = node;
14    }
15
16    private void removeNode(DLinkedNode node) {
17        DLinkedNode pre = node.pre;
18        DLinkedNode post = node.post;
19        pre.post = post;
20        post.pre = pre;
21    }
22
23    private void moveToHead(DLinkedNode node) {
24        this.removeNode(node);
25        this.addNode(node);
26    }
27
28    private DLinkedNode popTail() {
29        DLinkedNode res = tail.pre;
30        this.removeNode(res);
31        return res;
32    }
33
34    private Hashtable<Integer, DLinkedNode> cache = new Hashtable<Integer, DLinkedNode>();
35    private int count;
36    private int capacity;
37    private DLinkedNode head, tail;
38
39    public LRUCache1(int capacity) {
40        this.count = 0;
41        this.capacity = capacity;
42        head = new DLinkedNode();
43        head.pre = null;
44        tail = new DLinkedNode();
45        tail.post = null;
46        head.post = tail;
47        tail.pre = head;
48    }
49
50    public int get(int key) {
51        DLinkedNode node = cache.get(key);
52        if (node == null) {
53            return -1;
54        }
55        this.moveToHead(node);
56        return node.value;
57    }
58
59    public void put(int key, int value) {
60        DLinkedNode node = cache.get(key);
61        if (node == null) {
62            DLinkedNode newNode = new DLinkedNode();
63            newNode.key = key;
64            newNode.value = value;
65            this.cache.put(key, newNode);
66            this.addNode(newNode);
67            ++count;
68            if (count > capacity) {
69                DLinkedNode tail = this.popTail();
70                this.cache.remove(tail.key);
71                --count;
72            }
73        } else {
74            node.value = value;
75            this.moveToHead(node);
76        }
77    }
78}

解析:

LRUCache缓存,这里只需要实现get和put方法即可。需要两个指针。一个指向前一个,一个指向后一个。当我们使用某个元素的时候,如果缓存中有,我们只需要把它挪到最前面即可实现LURCache,原理很容易理解。我们我们熟悉LinkedHashMap源码,就会明白,他就是一个双向的环形链表,使用它也可以实现LRUCache,做android开发的同学可能知道,android中有个图片缓存的类也叫LRUCache,其实他内部封装的就是LinkedHashMap。我们来自己实现一个非常简单的

 1class LRUCache {
2    private LinkedHashMap<Integer, Integer> map;
3    private final int CAPACITY;
4
5    public LRUCache(int capacity) {
6        CAPACITY = capacity;
7        map = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
8            protected boolean removeEldestEntry(Map.Entry eldest) {
9                return size() > CAPACITY;
10            }
11        };
12    }
13
14    public int get(int key) {
15        return map.getOrDefault(key, -1);
16    }
17
18    public void set(int key, int value) {
19        map.put(key, value);
20    }
21}

 

标签:node,缓存,get,int,cache,DLinkedNode,LRU,key,278
来源: https://blog.51cto.com/u_4774266/2902501

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有