lru_cache.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. # lru_cache.py -- Simple LRU cache for dulwich
  2. # Copyright (C) 2006, 2008 Canonical Ltd
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """A simple least-recently-used (LRU) cache."""
  21. _null_key = object()
  22. class _LRUNode(object):
  23. """This maintains the linked-list which is the lru internals."""
  24. __slots__ = ("prev", "next_key", "key", "value", "cleanup", "size")
  25. def __init__(self, key, value, cleanup=None):
  26. self.prev = None
  27. self.next_key = _null_key
  28. self.key = key
  29. self.value = value
  30. self.cleanup = cleanup
  31. # TODO: We could compute this 'on-the-fly' like we used to, and remove
  32. # one pointer from this object, we just need to decide if it
  33. # actually costs us much of anything in normal usage
  34. self.size = None
  35. def __repr__(self):
  36. if self.prev is None:
  37. prev_key = None
  38. else:
  39. prev_key = self.prev.key
  40. return "%s(%r n:%r p:%r)" % (
  41. self.__class__.__name__,
  42. self.key,
  43. self.next_key,
  44. prev_key,
  45. )
  46. def run_cleanup(self):
  47. if self.cleanup is not None:
  48. self.cleanup(self.key, self.value)
  49. self.cleanup = None
  50. # Just make sure to break any refcycles, etc
  51. self.value = None
  52. class LRUCache(object):
  53. """A class which manages a cache of entries, removing unused ones."""
  54. def __init__(self, max_cache=100, after_cleanup_count=None):
  55. self._cache = {}
  56. # The "HEAD" of the lru linked list
  57. self._most_recently_used = None
  58. # The "TAIL" of the lru linked list
  59. self._least_recently_used = None
  60. self._update_max_cache(max_cache, after_cleanup_count)
  61. def __contains__(self, key):
  62. return key in self._cache
  63. def __getitem__(self, key):
  64. cache = self._cache
  65. node = cache[key]
  66. # Inlined from _record_access to decrease the overhead of __getitem__
  67. # We also have more knowledge about structure if __getitem__ is
  68. # succeeding, then we know that self._most_recently_used must not be
  69. # None, etc.
  70. mru = self._most_recently_used
  71. if node is mru:
  72. # Nothing to do, this node is already at the head of the queue
  73. return node.value
  74. # Remove this node from the old location
  75. node_prev = node.prev
  76. next_key = node.next_key
  77. # benchmarking shows that the lookup of _null_key in globals is faster
  78. # than the attribute lookup for (node is self._least_recently_used)
  79. if next_key is _null_key:
  80. # 'node' is the _least_recently_used, because it doesn't have a
  81. # 'next' item. So move the current lru to the previous node.
  82. self._least_recently_used = node_prev
  83. else:
  84. node_next = cache[next_key]
  85. node_next.prev = node_prev
  86. node_prev.next_key = next_key
  87. # Insert this node at the front of the list
  88. node.next_key = mru.key
  89. mru.prev = node
  90. self._most_recently_used = node
  91. node.prev = None
  92. return node.value
  93. def __len__(self):
  94. return len(self._cache)
  95. def _walk_lru(self):
  96. """Walk the LRU list, only meant to be used in tests."""
  97. node = self._most_recently_used
  98. if node is not None:
  99. if node.prev is not None:
  100. raise AssertionError(
  101. "the _most_recently_used entry is not"
  102. " supposed to have a previous entry"
  103. " %s" % (node,)
  104. )
  105. while node is not None:
  106. if node.next_key is _null_key:
  107. if node is not self._least_recently_used:
  108. raise AssertionError(
  109. "only the last node should have" " no next value: %s" % (node,)
  110. )
  111. node_next = None
  112. else:
  113. node_next = self._cache[node.next_key]
  114. if node_next.prev is not node:
  115. raise AssertionError(
  116. "inconsistency found, node.next.prev" " != node: %s" % (node,)
  117. )
  118. if node.prev is None:
  119. if node is not self._most_recently_used:
  120. raise AssertionError(
  121. "only the _most_recently_used should"
  122. " not have a previous node: %s" % (node,)
  123. )
  124. else:
  125. if node.prev.next_key != node.key:
  126. raise AssertionError(
  127. "inconsistency found, node.prev.next" " != node: %s" % (node,)
  128. )
  129. yield node
  130. node = node_next
  131. def add(self, key, value, cleanup=None):
  132. """Add a new value to the cache.
  133. Also, if the entry is ever removed from the cache, call
  134. cleanup(key, value).
  135. Args:
  136. key: The key to store it under
  137. value: The object to store
  138. cleanup: None or a function taking (key, value) to indicate
  139. 'value' should be cleaned up.
  140. """
  141. if key is _null_key:
  142. raise ValueError("cannot use _null_key as a key")
  143. if key in self._cache:
  144. node = self._cache[key]
  145. node.run_cleanup()
  146. node.value = value
  147. node.cleanup = cleanup
  148. else:
  149. node = _LRUNode(key, value, cleanup=cleanup)
  150. self._cache[key] = node
  151. self._record_access(node)
  152. if len(self._cache) > self._max_cache:
  153. # Trigger the cleanup
  154. self.cleanup()
  155. def cache_size(self):
  156. """Get the number of entries we will cache."""
  157. return self._max_cache
  158. def get(self, key, default=None):
  159. node = self._cache.get(key, None)
  160. if node is None:
  161. return default
  162. self._record_access(node)
  163. return node.value
  164. def keys(self):
  165. """Get the list of keys currently cached.
  166. Note that values returned here may not be available by the time you
  167. request them later. This is simply meant as a peak into the current
  168. state.
  169. Returns: An unordered list of keys that are currently cached.
  170. """
  171. return self._cache.keys()
  172. def items(self):
  173. """Get the key:value pairs as a dict."""
  174. return {k: n.value for k, n in self._cache.items()}
  175. def cleanup(self):
  176. """Clear the cache until it shrinks to the requested size.
  177. This does not completely wipe the cache, just makes sure it is under
  178. the after_cleanup_count.
  179. """
  180. # Make sure the cache is shrunk to the correct size
  181. while len(self._cache) > self._after_cleanup_count:
  182. self._remove_lru()
  183. def __setitem__(self, key, value):
  184. """Add a value to the cache, there will be no cleanup function."""
  185. self.add(key, value, cleanup=None)
  186. def _record_access(self, node):
  187. """Record that key was accessed."""
  188. # Move 'node' to the front of the queue
  189. if self._most_recently_used is None:
  190. self._most_recently_used = node
  191. self._least_recently_used = node
  192. return
  193. elif node is self._most_recently_used:
  194. # Nothing to do, this node is already at the head of the queue
  195. return
  196. # We've taken care of the tail pointer, remove the node, and insert it
  197. # at the front
  198. # REMOVE
  199. if node is self._least_recently_used:
  200. self._least_recently_used = node.prev
  201. if node.prev is not None:
  202. node.prev.next_key = node.next_key
  203. if node.next_key is not _null_key:
  204. node_next = self._cache[node.next_key]
  205. node_next.prev = node.prev
  206. # INSERT
  207. node.next_key = self._most_recently_used.key
  208. self._most_recently_used.prev = node
  209. self._most_recently_used = node
  210. node.prev = None
  211. def _remove_node(self, node):
  212. if node is self._least_recently_used:
  213. self._least_recently_used = node.prev
  214. self._cache.pop(node.key)
  215. # If we have removed all entries, remove the head pointer as well
  216. if self._least_recently_used is None:
  217. self._most_recently_used = None
  218. node.run_cleanup()
  219. # Now remove this node from the linked list
  220. if node.prev is not None:
  221. node.prev.next_key = node.next_key
  222. if node.next_key is not _null_key:
  223. node_next = self._cache[node.next_key]
  224. node_next.prev = node.prev
  225. # And remove this node's pointers
  226. node.prev = None
  227. node.next_key = _null_key
  228. def _remove_lru(self):
  229. """Remove one entry from the lru, and handle consequences.
  230. If there are no more references to the lru, then this entry should be
  231. removed from the cache.
  232. """
  233. self._remove_node(self._least_recently_used)
  234. def clear(self):
  235. """Clear out all of the cache."""
  236. # Clean up in LRU order
  237. while self._cache:
  238. self._remove_lru()
  239. def resize(self, max_cache, after_cleanup_count=None):
  240. """Change the number of entries that will be cached."""
  241. self._update_max_cache(max_cache, after_cleanup_count=after_cleanup_count)
  242. def _update_max_cache(self, max_cache, after_cleanup_count=None):
  243. self._max_cache = max_cache
  244. if after_cleanup_count is None:
  245. self._after_cleanup_count = self._max_cache * 8 / 10
  246. else:
  247. self._after_cleanup_count = min(after_cleanup_count, self._max_cache)
  248. self.cleanup()
  249. class LRUSizeCache(LRUCache):
  250. """An LRUCache that removes things based on the size of the values.
  251. This differs in that it doesn't care how many actual items there are,
  252. it just restricts the cache to be cleaned up after so much data is stored.
  253. The size of items added will be computed using compute_size(value), which
  254. defaults to len() if not supplied.
  255. """
  256. def __init__(
  257. self, max_size=1024 * 1024, after_cleanup_size=None, compute_size=None
  258. ):
  259. """Create a new LRUSizeCache.
  260. Args:
  261. max_size: The max number of bytes to store before we start
  262. clearing out entries.
  263. after_cleanup_size: After cleaning up, shrink everything to this
  264. size.
  265. compute_size: A function to compute the size of the values. We
  266. use a function here, so that you can pass 'len' if you are just
  267. using simple strings, or a more complex function if you are using
  268. something like a list of strings, or even a custom object.
  269. The function should take the form "compute_size(value) => integer".
  270. If not supplied, it defaults to 'len()'
  271. """
  272. self._value_size = 0
  273. self._compute_size = compute_size
  274. if compute_size is None:
  275. self._compute_size = len
  276. self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
  277. LRUCache.__init__(self, max_cache=max(int(max_size / 512), 1))
  278. def add(self, key, value, cleanup=None):
  279. """Add a new value to the cache.
  280. Also, if the entry is ever removed from the cache, call
  281. cleanup(key, value).
  282. Args:
  283. key: The key to store it under
  284. value: The object to store
  285. cleanup: None or a function taking (key, value) to indicate
  286. 'value' should be cleaned up.
  287. """
  288. if key is _null_key:
  289. raise ValueError("cannot use _null_key as a key")
  290. node = self._cache.get(key, None)
  291. value_len = self._compute_size(value)
  292. if value_len >= self._after_cleanup_size:
  293. # The new value is 'too big to fit', as it would fill up/overflow
  294. # the cache all by itself
  295. if node is not None:
  296. # We won't be replacing the old node, so just remove it
  297. self._remove_node(node)
  298. if cleanup is not None:
  299. cleanup(key, value)
  300. return
  301. if node is None:
  302. node = _LRUNode(key, value, cleanup=cleanup)
  303. self._cache[key] = node
  304. else:
  305. self._value_size -= node.size
  306. node.size = value_len
  307. self._value_size += value_len
  308. self._record_access(node)
  309. if self._value_size > self._max_size:
  310. # Time to cleanup
  311. self.cleanup()
  312. def cleanup(self):
  313. """Clear the cache until it shrinks to the requested size.
  314. This does not completely wipe the cache, just makes sure it is under
  315. the after_cleanup_size.
  316. """
  317. # Make sure the cache is shrunk to the correct size
  318. while self._value_size > self._after_cleanup_size:
  319. self._remove_lru()
  320. def _remove_node(self, node):
  321. self._value_size -= node.size
  322. LRUCache._remove_node(self, node)
  323. def resize(self, max_size, after_cleanup_size=None):
  324. """Change the number of bytes that will be cached."""
  325. self._update_max_size(max_size, after_cleanup_size=after_cleanup_size)
  326. max_cache = max(int(max_size / 512), 1)
  327. self._update_max_cache(max_cache)
  328. def _update_max_size(self, max_size, after_cleanup_size=None):
  329. self._max_size = max_size
  330. if after_cleanup_size is None:
  331. self._after_cleanup_size = self._max_size * 8 // 10
  332. else:
  333. self._after_cleanup_size = min(after_cleanup_size, self._max_size)