美文网首页
设计一个LRU

设计一个LRU

作者: MontyOak | 来源:发表于2018-07-07 21:24 被阅读16次

原文链接
一个更好的实现
作为演示代码,做了以下的简化处理:

  • 缓存结果是网络查询结果
  • 不对输入进行校验
  • 假设内存能够存放所有内容
class Node(object):

    def __init__(self, results):
        self.results = results
        self.prev = None
        self.next = None


class LinkedList(object):

    def __init__(self):
        self.head = None
        self.tail = None

    def move_to_front(self, node):  # ...
    def append_to_front(self, node):  # ...
    def remove_from_tail(self):  # ...


class Cache(object):

    def __init__(self, MAX_SIZE):
        self.MAX_SIZE = MAX_SIZE
        self.size = 0
        self.lookup = {}  # key: query, value: node
        self.linked_list = LinkedList()

    def get(self, query)
        """Get the stored query result from the cache.
        
        Accessing a node updates its position to the front of the LRU list.
        """
        node = self.lookup.get(query)
        if node is None:
            return None
        self.linked_list.move_to_front(node)
        return node.results

    def set(self, results, query):
        """Set the result for the given query key in the cache.
        
        When updating an entry, updates its position to the front of the LRU list.
        If the entry is new and the cache is at capacity, removes the oldest entry
        before the new entry is added.
        """
        node = self.lookup.get(query)
        if node is not None:
            # Key exists in cache, update the value
            node.results = results
            self.linked_list.move_to_front(node)
        else:
            # Key does not exist in cache
            if self.size == self.MAX_SIZE:
                # Remove the oldest entry from the linked list and lookup
                self.lookup.pop(self.linked_list.tail.query, None)
                self.linked_list.remove_from_tail()
            else:
                self.size += 1
            # Add the new key and value
            new_node = Node(results)
            self.linked_list.append_to_front(new_node)
            self.lookup[query] = new_node

从数据结构上来讲,主要用到的是链表(比较好的方式其实是双链表,有时会配合哈希表来提升查询效率)。
下面是Leetcode上的题目参考解法:

class List(object):
    @staticmethod
    def delete(elem):
        elem.prev.next = elem.next
        elem.next.prev = elem.prev
        return elem
    
    @staticmethod   
    def move(elem, newPrev, newNext):
        elem.prev = newPrev
        elem.next = newNext
        newPrev.next = elem
        newNext.prev = elem
        
    @staticmethod
    def append(head, elem):
        List.move(elem, head.prev, head)
    
    @staticmethod
    def isEmpty(head):
        return head.next == head.prev == head
        
    @staticmethod
    def initHead(head):
        head.prev = head.next = head

class Node(object):
    def __init__(self, key, value, head):
        self.key = key
        self.value = value
        self.head = head
        self.prev = self.next = None
        
    def hit(self):
        List.delete(self)
        List.append(self.head, self)

class LRUCache(object):
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.d = {}
        self.cap = capacity
        self.head = Node(-1, -1, None)
        List.initHead(self.head)

    def get(self, key):
        """
        :rtype: int
        """
        if key not in self.d:
            return -1
        self.d[key].hit()
        return self.d[key].value
        

    def set(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """
        if self.cap == 0:
            return
        
        if key in self.d:
            self.d[key].hit()
            self.d[key].value = value
        else:
            if len(self.d) >= self.cap:
                oldNode = List.delete(self.head.next)
                del self.d[oldNode.key]
                
            newNode = Node(key, value, self.head)
            List.append(self.head, newNode)
            self.d[key] = newNode

相关文章

  • Cache实现

    LRU算法的实现 https://leetcode.com/problems/lru-cache/1.设计一个缓存...

  • 146. LRU 缓存

    146. LRU 缓存[https://leetcode.cn/problems/lru-cache/] 请你设计...

  • 算法第4天:LRU缓存机制

    leetcode 146. LRU缓存机制 middle 运用你所掌握的数据结构,设计和实现一个 LRU (最...

  • LeetCode热门100题算法和思路(day6)

    LeetCode 146 LRU缓存 题目详情 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) ...

  • 2018-11-11 LRU 算法一

    LRU全称是Least Recently Used,即最近最久未使用的意思。 LRU算法的设计原则是:如果一个数据...

  • LRU缓存

    146. LRU缓存 设计和实现一个LRU(最近最少使用)的缓存机制。它可以支持以下操作: get 和 put 。...

  • LeetCode-146- LRU 缓存机制

    LRU 缓存机制 题目描述:运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。实现 ...

  • 力扣(LeetCode) -146 LRU缓存机制

    本题考察的LRU缓存机制,HashMap和链表 题目描述 运用你所掌握的数据结构,设计和实现一个 LRU (最近...

  • 设计一个LRU

    原文链接一个更好的实现作为演示代码,做了以下的简化处理: 缓存结果是网络查询结果 不对输入进行校验 假设内存能够存...

  • 算法笔记——LRU和LFU缓存结构

    LRU缓存结构 问题描述: 设计可以变更的缓存结构(LRU)【题目】设计一种缓存结构,该结构在构造时确定大小,假设...

网友评论

      本文标题:设计一个LRU

      本文链接:https://www.haomeiwen.com/subject/citnuftx.html