设计一个LRU
2018-07-07 本文已影响16人
MontyOak
原文链接
一个更好的实现
作为演示代码,做了以下的简化处理:
- 缓存结果是网络查询结果
- 不对输入进行校验
- 假设内存能够存放所有内容
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