2
0

lru_cache.py 14 KB

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