lru_cache.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. class Node(object):
  2. def __init__(self, results):
  3. self.results = results
  4. self.next = next
  5. class LinkedList(object):
  6. def __init__(self):
  7. self.head = None
  8. self.tail = None
  9. def move_to_front(self, node): # ...
  10. def append_to_front(self, node): # ...
  11. def remove_from_tail(self): # ...
  12. class Cache(object):
  13. def __init__(self, MAX_SIZE):
  14. self.MAX_SIZE = MAX_SIZE
  15. self.size = 0
  16. self.lookup = {} # key: query, value: node
  17. self.linked_list = LinkedList()
  18. def get(self, query)
  19. """Get the stored query result from the cache.
  20. Accessing a node updates its position to the front of the LRU list.
  21. """
  22. node = self.lookup[query]
  23. if node is None:
  24. return None
  25. self.linked_list.move_to_front(node)
  26. return node.results
  27. def set(self, results, query):
  28. """Set the result for the given query key in the cache.
  29. When updating an entry, updates its position to the front of the LRU list.
  30. If the entry is new and the cache is at capacity, removes the oldest entry
  31. before the new entry is added.
  32. """
  33. node = self.lookup[query]
  34. if node is not None:
  35. # Key exists in cache, update the value
  36. node.results = results
  37. self.linked_list.move_to_front(node)
  38. else:
  39. # Key does not exist in cache
  40. if self.size == self.MAX_SIZE:
  41. # Remove the oldest entry from the linked list and lookup
  42. self.lookup.pop(self.linked_list.tail.query, None)
  43. self.linked_list.remove_from_tail()
  44. else:
  45. self.size += 1
  46. # Add the new key and value
  47. new_node = Node(results)
  48. self.linked_list.append_to_front(new_node)
  49. self.lookup[query] = new_node