lrucache.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from functools import wraps
  16. import threading
  17. from synapse.util.caches.treecache import TreeCache
  18. def enumerate_leaves(node, depth):
  19. if depth == 0:
  20. yield node
  21. else:
  22. for n in node.values():
  23. for m in enumerate_leaves(n, depth - 1):
  24. yield m
  25. class _Node(object):
  26. __slots__ = ["prev_node", "next_node", "key", "value", "callbacks"]
  27. def __init__(self, prev_node, next_node, key, value, callbacks=set()):
  28. self.prev_node = prev_node
  29. self.next_node = next_node
  30. self.key = key
  31. self.value = value
  32. self.callbacks = callbacks
  33. class LruCache(object):
  34. """
  35. Least-recently-used cache.
  36. Supports del_multi only if cache_type=TreeCache
  37. If cache_type=TreeCache, all keys must be tuples.
  38. Can also set callbacks on objects when getting/setting which are fired
  39. when that key gets invalidated/evicted.
  40. """
  41. def __init__(self, max_size, keylen=1, cache_type=dict, size_callback=None,
  42. evicted_callback=None):
  43. """
  44. Args:
  45. max_size (int):
  46. keylen (int):
  47. cache_type (type):
  48. type of underlying cache to be used. Typically one of dict
  49. or TreeCache.
  50. size_callback (func(V) -> int | None):
  51. evicted_callback (func(int)|None):
  52. if not None, called on eviction with the size of the evicted
  53. entry
  54. """
  55. cache = cache_type()
  56. self.cache = cache # Used for introspection.
  57. list_root = _Node(None, None, None, None)
  58. list_root.next_node = list_root
  59. list_root.prev_node = list_root
  60. lock = threading.Lock()
  61. def evict():
  62. while cache_len() > max_size:
  63. todelete = list_root.prev_node
  64. evicted_len = delete_node(todelete)
  65. cache.pop(todelete.key, None)
  66. if evicted_callback:
  67. evicted_callback(evicted_len)
  68. def synchronized(f):
  69. @wraps(f)
  70. def inner(*args, **kwargs):
  71. with lock:
  72. return f(*args, **kwargs)
  73. return inner
  74. cached_cache_len = [0]
  75. if size_callback is not None:
  76. def cache_len():
  77. return cached_cache_len[0]
  78. else:
  79. def cache_len():
  80. return len(cache)
  81. self.len = synchronized(cache_len)
  82. def add_node(key, value, callbacks=set()):
  83. prev_node = list_root
  84. next_node = prev_node.next_node
  85. node = _Node(prev_node, next_node, key, value, callbacks)
  86. prev_node.next_node = node
  87. next_node.prev_node = node
  88. cache[key] = node
  89. if size_callback:
  90. cached_cache_len[0] += size_callback(node.value)
  91. def move_node_to_front(node):
  92. prev_node = node.prev_node
  93. next_node = node.next_node
  94. prev_node.next_node = next_node
  95. next_node.prev_node = prev_node
  96. prev_node = list_root
  97. next_node = prev_node.next_node
  98. node.prev_node = prev_node
  99. node.next_node = next_node
  100. prev_node.next_node = node
  101. next_node.prev_node = node
  102. def delete_node(node):
  103. prev_node = node.prev_node
  104. next_node = node.next_node
  105. prev_node.next_node = next_node
  106. next_node.prev_node = prev_node
  107. deleted_len = 1
  108. if size_callback:
  109. deleted_len = size_callback(node.value)
  110. cached_cache_len[0] -= deleted_len
  111. for cb in node.callbacks:
  112. cb()
  113. node.callbacks.clear()
  114. return deleted_len
  115. @synchronized
  116. def cache_get(key, default=None, callbacks=[]):
  117. node = cache.get(key, None)
  118. if node is not None:
  119. move_node_to_front(node)
  120. node.callbacks.update(callbacks)
  121. return node.value
  122. else:
  123. return default
  124. @synchronized
  125. def cache_set(key, value, callbacks=[]):
  126. node = cache.get(key, None)
  127. if node is not None:
  128. # We sometimes store large objects, e.g. dicts, which cause
  129. # the inequality check to take a long time. So let's only do
  130. # the check if we have some callbacks to call.
  131. if node.callbacks and value != node.value:
  132. for cb in node.callbacks:
  133. cb()
  134. node.callbacks.clear()
  135. # We don't bother to protect this by value != node.value as
  136. # generally size_callback will be cheap compared with equality
  137. # checks. (For example, taking the size of two dicts is quicker
  138. # than comparing them for equality.)
  139. if size_callback:
  140. cached_cache_len[0] -= size_callback(node.value)
  141. cached_cache_len[0] += size_callback(value)
  142. node.callbacks.update(callbacks)
  143. move_node_to_front(node)
  144. node.value = value
  145. else:
  146. add_node(key, value, set(callbacks))
  147. evict()
  148. @synchronized
  149. def cache_set_default(key, value):
  150. node = cache.get(key, None)
  151. if node is not None:
  152. return node.value
  153. else:
  154. add_node(key, value)
  155. evict()
  156. return value
  157. @synchronized
  158. def cache_pop(key, default=None):
  159. node = cache.get(key, None)
  160. if node:
  161. delete_node(node)
  162. cache.pop(node.key, None)
  163. return node.value
  164. else:
  165. return default
  166. @synchronized
  167. def cache_del_multi(key):
  168. """
  169. This will only work if constructed with cache_type=TreeCache
  170. """
  171. popped = cache.pop(key)
  172. if popped is None:
  173. return
  174. for leaf in enumerate_leaves(popped, keylen - len(key)):
  175. delete_node(leaf)
  176. @synchronized
  177. def cache_clear():
  178. list_root.next_node = list_root
  179. list_root.prev_node = list_root
  180. for node in cache.values():
  181. for cb in node.callbacks:
  182. cb()
  183. cache.clear()
  184. if size_callback:
  185. cached_cache_len[0] = 0
  186. @synchronized
  187. def cache_contains(key):
  188. return key in cache
  189. self.sentinel = object()
  190. self.get = cache_get
  191. self.set = cache_set
  192. self.setdefault = cache_set_default
  193. self.pop = cache_pop
  194. if cache_type is TreeCache:
  195. self.del_multi = cache_del_multi
  196. self.len = synchronized(cache_len)
  197. self.contains = cache_contains
  198. self.clear = cache_clear
  199. def __getitem__(self, key):
  200. result = self.get(key, self.sentinel)
  201. if result is self.sentinel:
  202. raise KeyError()
  203. else:
  204. return result
  205. def __setitem__(self, key, value):
  206. self.set(key, value)
  207. def __delitem__(self, key, value):
  208. result = self.pop(key, self.sentinel)
  209. if result is self.sentinel:
  210. raise KeyError()
  211. def __len__(self):
  212. return self.len()
  213. def __contains__(self, key):
  214. return self.contains(key)