lru_cache.py 1.9 KB

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