2015-02-11 07:52:23 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-02-11 07:52:23 -07:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
from functools import wraps
|
|
|
|
import threading
|
|
|
|
|
2016-01-22 06:40:37 -07:00
|
|
|
from synapse.util.caches.treecache import TreeCache
|
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
|
|
|
|
def enumerate_leaves(node, depth):
|
|
|
|
if depth == 0:
|
|
|
|
yield node
|
|
|
|
else:
|
|
|
|
for n in node.values():
|
|
|
|
for m in enumerate_leaves(n, depth - 1):
|
|
|
|
yield m
|
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
|
2016-03-22 10:06:21 -06:00
|
|
|
class _Node(object):
|
2016-08-19 04:18:26 -06:00
|
|
|
__slots__ = ["prev_node", "next_node", "key", "value", "callbacks"]
|
2016-03-22 10:06:21 -06:00
|
|
|
|
2016-08-19 08:58:52 -06:00
|
|
|
def __init__(self, prev_node, next_node, key, value, callbacks=set()):
|
2016-03-22 10:06:21 -06:00
|
|
|
self.prev_node = prev_node
|
|
|
|
self.next_node = next_node
|
|
|
|
self.key = key
|
|
|
|
self.value = value
|
2016-08-19 04:18:26 -06:00
|
|
|
self.callbacks = callbacks
|
2016-03-22 10:06:21 -06:00
|
|
|
|
|
|
|
|
2015-02-11 07:52:23 -07:00
|
|
|
class LruCache(object):
|
2016-01-22 05:21:13 -07:00
|
|
|
"""
|
|
|
|
Least-recently-used cache.
|
|
|
|
Supports del_multi only if cache_type=TreeCache
|
|
|
|
If cache_type=TreeCache, all keys must be tuples.
|
2016-08-19 04:18:26 -06:00
|
|
|
|
|
|
|
Can also set callbacks on objects when getting/setting which are fired
|
|
|
|
when that key gets invalidated/evicted.
|
2016-01-22 05:21:13 -07:00
|
|
|
"""
|
2017-01-13 10:46:17 -07:00
|
|
|
def __init__(self, max_size, keylen=1, cache_type=dict, size_callback=None):
|
2016-01-22 05:10:33 -07:00
|
|
|
cache = cache_type()
|
2016-01-29 03:00:45 -07:00
|
|
|
self.cache = cache # Used for introspection.
|
2016-03-22 10:06:21 -06:00
|
|
|
list_root = _Node(None, None, None, None)
|
|
|
|
list_root.next_node = list_root
|
|
|
|
list_root.prev_node = list_root
|
2015-02-11 07:52:23 -07:00
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
lock = threading.Lock()
|
|
|
|
|
2017-01-13 10:46:17 -07:00
|
|
|
def evict():
|
|
|
|
while cache_len() > max_size:
|
|
|
|
todelete = list_root.prev_node
|
|
|
|
delete_node(todelete)
|
|
|
|
cache.pop(todelete.key, None)
|
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
def synchronized(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
with lock:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
2017-01-17 04:18:13 -07:00
|
|
|
cached_cache_len = [0]
|
|
|
|
if size_callback is not None:
|
|
|
|
def cache_len():
|
|
|
|
return cached_cache_len[0]
|
|
|
|
else:
|
|
|
|
def cache_len():
|
|
|
|
return len(cache)
|
|
|
|
|
|
|
|
self.len = synchronized(cache_len)
|
|
|
|
|
2016-08-19 08:58:52 -06:00
|
|
|
def add_node(key, value, callbacks=set()):
|
2015-02-11 07:52:23 -07:00
|
|
|
prev_node = list_root
|
2016-03-22 10:06:21 -06:00
|
|
|
next_node = prev_node.next_node
|
2016-08-19 04:18:26 -06:00
|
|
|
node = _Node(prev_node, next_node, key, value, callbacks)
|
2016-03-22 10:06:21 -06:00
|
|
|
prev_node.next_node = node
|
|
|
|
next_node.prev_node = node
|
2015-02-11 07:52:23 -07:00
|
|
|
cache[key] = node
|
|
|
|
|
2017-01-17 04:18:13 -07:00
|
|
|
if size_callback:
|
|
|
|
cached_cache_len[0] += size_callback(node.value)
|
|
|
|
|
2015-02-11 07:52:23 -07:00
|
|
|
def move_node_to_front(node):
|
2016-03-22 10:06:21 -06:00
|
|
|
prev_node = node.prev_node
|
|
|
|
next_node = node.next_node
|
|
|
|
prev_node.next_node = next_node
|
|
|
|
next_node.prev_node = prev_node
|
2015-02-11 07:52:23 -07:00
|
|
|
prev_node = list_root
|
2016-03-22 10:06:21 -06:00
|
|
|
next_node = prev_node.next_node
|
|
|
|
node.prev_node = prev_node
|
|
|
|
node.next_node = next_node
|
|
|
|
prev_node.next_node = node
|
|
|
|
next_node.prev_node = node
|
2015-02-11 07:52:23 -07:00
|
|
|
|
|
|
|
def delete_node(node):
|
2016-03-22 10:06:21 -06:00
|
|
|
prev_node = node.prev_node
|
|
|
|
next_node = node.next_node
|
|
|
|
prev_node.next_node = next_node
|
|
|
|
next_node.prev_node = prev_node
|
2015-02-11 07:52:23 -07:00
|
|
|
|
2017-01-17 04:18:13 -07:00
|
|
|
if size_callback:
|
|
|
|
cached_cache_len[0] -= size_callback(node.value)
|
|
|
|
|
2016-08-19 04:18:26 -06:00
|
|
|
for cb in node.callbacks:
|
|
|
|
cb()
|
2016-08-19 08:58:52 -06:00
|
|
|
node.callbacks.clear()
|
2016-08-19 04:18:26 -06:00
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
@synchronized
|
2017-01-17 04:18:13 -07:00
|
|
|
def cache_get(key, default=None, callbacks=[]):
|
2015-02-11 07:52:23 -07:00
|
|
|
node = cache.get(key, None)
|
|
|
|
if node is not None:
|
|
|
|
move_node_to_front(node)
|
2017-01-17 04:18:13 -07:00
|
|
|
node.callbacks.update(callbacks)
|
2016-03-22 10:06:21 -06:00
|
|
|
return node.value
|
2015-02-11 07:52:23 -07:00
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
@synchronized
|
2017-01-17 04:18:13 -07:00
|
|
|
def cache_set(key, value, callbacks=[]):
|
2015-02-11 07:52:23 -07:00
|
|
|
node = cache.get(key, None)
|
|
|
|
if node is not None:
|
2016-08-19 04:18:26 -06:00
|
|
|
if value != node.value:
|
|
|
|
for cb in node.callbacks:
|
|
|
|
cb()
|
2016-08-19 08:58:52 -06:00
|
|
|
node.callbacks.clear()
|
2016-08-19 04:18:26 -06:00
|
|
|
|
2017-01-17 04:18:13 -07:00
|
|
|
if size_callback:
|
|
|
|
cached_cache_len[0] -= size_callback(node.value)
|
|
|
|
cached_cache_len[0] += size_callback(value)
|
|
|
|
|
|
|
|
node.callbacks.update(callbacks)
|
2016-08-19 04:18:26 -06:00
|
|
|
|
2015-02-11 07:52:23 -07:00
|
|
|
move_node_to_front(node)
|
2016-03-22 10:06:21 -06:00
|
|
|
node.value = value
|
2015-02-11 07:52:23 -07:00
|
|
|
else:
|
2017-01-17 04:18:13 -07:00
|
|
|
add_node(key, value, set(callbacks))
|
2017-01-13 10:46:17 -07:00
|
|
|
|
|
|
|
evict()
|
2015-02-11 07:52:23 -07:00
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
@synchronized
|
2015-02-11 07:52:23 -07:00
|
|
|
def cache_set_default(key, value):
|
|
|
|
node = cache.get(key, None)
|
|
|
|
if node is not None:
|
2016-03-22 10:06:21 -06:00
|
|
|
return node.value
|
2015-02-11 07:52:23 -07:00
|
|
|
else:
|
|
|
|
add_node(key, value)
|
2017-01-13 10:46:17 -07:00
|
|
|
evict()
|
2015-02-11 07:52:23 -07:00
|
|
|
return value
|
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
@synchronized
|
2015-02-11 07:52:23 -07:00
|
|
|
def cache_pop(key, default=None):
|
|
|
|
node = cache.get(key, None)
|
|
|
|
if node:
|
|
|
|
delete_node(node)
|
2016-03-22 10:06:21 -06:00
|
|
|
cache.pop(node.key, None)
|
|
|
|
return node.value
|
2015-02-11 07:52:23 -07:00
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
2016-01-21 12:16:25 -07:00
|
|
|
@synchronized
|
|
|
|
def cache_del_multi(key):
|
2016-01-22 05:10:33 -07:00
|
|
|
"""
|
|
|
|
This will only work if constructed with cache_type=TreeCache
|
|
|
|
"""
|
2016-01-21 12:16:25 -07:00
|
|
|
popped = cache.pop(key)
|
|
|
|
if popped is None:
|
|
|
|
return
|
|
|
|
for leaf in enumerate_leaves(popped, keylen - len(key)):
|
|
|
|
delete_node(leaf)
|
|
|
|
|
2015-05-21 04:13:19 -06:00
|
|
|
@synchronized
|
|
|
|
def cache_clear():
|
2016-03-22 10:06:21 -06:00
|
|
|
list_root.next_node = list_root
|
|
|
|
list_root.prev_node = list_root
|
2016-08-19 04:18:26 -06:00
|
|
|
for node in cache.values():
|
|
|
|
for cb in node.callbacks:
|
|
|
|
cb()
|
2015-05-21 04:13:19 -06:00
|
|
|
cache.clear()
|
2017-01-18 07:55:23 -07:00
|
|
|
if size_callback:
|
|
|
|
cached_cache_len[0] = 0
|
2015-05-21 04:13:19 -06:00
|
|
|
|
2015-04-15 09:07:59 -06:00
|
|
|
@synchronized
|
2015-03-25 13:04:59 -06:00
|
|
|
def cache_contains(key):
|
|
|
|
return key in cache
|
|
|
|
|
2015-02-11 07:52:23 -07:00
|
|
|
self.sentinel = object()
|
|
|
|
self.get = cache_get
|
|
|
|
self.set = cache_set
|
|
|
|
self.setdefault = cache_set_default
|
|
|
|
self.pop = cache_pop
|
2016-01-22 06:40:37 -07:00
|
|
|
if cache_type is TreeCache:
|
|
|
|
self.del_multi = cache_del_multi
|
2017-01-13 10:46:17 -07:00
|
|
|
self.len = synchronized(cache_len)
|
2015-03-25 13:04:59 -06:00
|
|
|
self.contains = cache_contains
|
2015-05-21 04:13:19 -06:00
|
|
|
self.clear = cache_clear
|
2015-02-11 07:52:23 -07:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
result = self.get(key, self.sentinel)
|
|
|
|
if result is self.sentinel:
|
|
|
|
raise KeyError()
|
|
|
|
else:
|
|
|
|
return result
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.set(key, value)
|
|
|
|
|
|
|
|
def __delitem__(self, key, value):
|
|
|
|
result = self.pop(key, self.sentinel)
|
|
|
|
if result is self.sentinel:
|
|
|
|
raise KeyError()
|
2015-03-04 10:32:28 -07:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.len()
|
2015-03-25 13:04:59 -06:00
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return self.contains(key)
|