2015-02-16 11:16:47 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-02-16 11:16:47 -05: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.
|
|
|
|
|
|
|
|
import logging
|
2018-07-09 02:09:20 -04:00
|
|
|
from collections import OrderedDict
|
2015-02-16 11:16:47 -05:00
|
|
|
|
2018-09-21 11:25:42 -04:00
|
|
|
from six import iteritems, itervalues
|
2018-09-21 09:52:21 -04:00
|
|
|
|
2018-07-18 09:35:24 -04:00
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util.caches import register_cache
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-09-21 09:55:47 -04:00
|
|
|
SENTINEL = object()
|
|
|
|
|
|
|
|
|
2015-02-16 11:16:47 -05:00
|
|
|
class ExpiringCache(object):
|
|
|
|
def __init__(self, cache_name, clock, max_len=0, expiry_ms=0,
|
2017-01-16 08:48:04 -05:00
|
|
|
reset_expiry_on_get=False, iterable=False):
|
2015-02-16 11:16:47 -05:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
cache_name (str): Name of this cache, used for logging.
|
|
|
|
clock (Clock)
|
|
|
|
max_len (int): Max size of dict. If the dict grows larger than this
|
|
|
|
then the oldest items get automatically evicted. Default is 0,
|
|
|
|
which indicates there is no max limit.
|
|
|
|
expiry_ms (int): How long before an item is evicted from the cache
|
|
|
|
in milliseconds. Default is 0, indicating items never get
|
|
|
|
evicted based on time.
|
|
|
|
reset_expiry_on_get (bool): If true, will reset the expiry time for
|
|
|
|
an item on access. Defaults to False.
|
2017-01-16 08:48:04 -05:00
|
|
|
iterable (bool): If true, the size is calculated by summing the
|
|
|
|
sizes of all entries, rather than the number of entries.
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
"""
|
|
|
|
self._cache_name = cache_name
|
|
|
|
|
|
|
|
self._clock = clock
|
|
|
|
|
|
|
|
self._max_len = max_len
|
|
|
|
self._expiry_ms = expiry_ms
|
|
|
|
|
|
|
|
self._reset_expiry_on_get = reset_expiry_on_get
|
|
|
|
|
2017-01-16 10:33:22 -05:00
|
|
|
self._cache = OrderedDict()
|
2015-02-16 11:16:47 -05:00
|
|
|
|
2017-01-16 08:48:04 -05:00
|
|
|
self.iterable = iterable
|
2016-02-23 06:54:48 -05:00
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
self.metrics = register_cache("expiring", cache_name, self)
|
|
|
|
|
2015-02-16 11:16:47 -05:00
|
|
|
if not self._expiry_ms:
|
|
|
|
# Don't bother starting the loop if things never expire
|
|
|
|
return
|
|
|
|
|
|
|
|
def f():
|
2018-07-26 06:44:26 -04:00
|
|
|
return run_as_background_process(
|
2018-07-18 09:35:24 -04:00
|
|
|
"prune_cache_%s" % self._cache_name,
|
|
|
|
self._prune_cache,
|
|
|
|
)
|
2015-02-16 11:16:47 -05:00
|
|
|
|
2016-02-02 12:18:50 -05:00
|
|
|
self._clock.looping_call(f, self._expiry_ms / 2)
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
now = self._clock.time_msec()
|
|
|
|
self._cache[key] = _CacheEntry(now, value)
|
|
|
|
|
|
|
|
# Evict if there are now too many items
|
2017-01-16 10:33:22 -05:00
|
|
|
while self._max_len and len(self) > self._max_len:
|
2017-01-17 06:18:13 -05:00
|
|
|
_key, value = self._cache.popitem(last=False)
|
|
|
|
if self.iterable:
|
2018-09-21 11:25:42 -04:00
|
|
|
self.metrics.inc_evictions(len(value.value))
|
2018-02-01 12:57:51 -05:00
|
|
|
else:
|
|
|
|
self.metrics.inc_evictions()
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
2016-02-23 06:54:48 -05:00
|
|
|
try:
|
|
|
|
entry = self._cache[key]
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_hits()
|
2016-03-01 05:59:17 -05:00
|
|
|
except KeyError:
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_misses()
|
2016-03-01 06:00:10 -05:00
|
|
|
raise
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
if self._reset_expiry_on_get:
|
|
|
|
entry.time = self._clock.time_msec()
|
|
|
|
|
|
|
|
return entry.value
|
|
|
|
|
2018-09-21 09:55:47 -04:00
|
|
|
def pop(self, key, default=None):
|
|
|
|
value = self._cache.pop(key, SENTINEL)
|
|
|
|
if value is SENTINEL:
|
|
|
|
return default
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
2017-06-29 09:08:33 -04:00
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self._cache
|
|
|
|
|
2015-02-16 11:16:47 -05:00
|
|
|
def get(self, key, default=None):
|
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
2017-03-10 05:30:38 -05:00
|
|
|
def setdefault(self, key, value):
|
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
self[key] = value
|
|
|
|
return value
|
|
|
|
|
2015-02-17 10:44:41 -05:00
|
|
|
def _prune_cache(self):
|
2015-02-16 11:16:47 -05:00
|
|
|
if not self._expiry_ms:
|
|
|
|
# zero expiry time means don't expire. This should never get called
|
|
|
|
# since we have this check in start too.
|
|
|
|
return
|
2017-01-16 08:48:04 -05:00
|
|
|
begin_length = len(self)
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
now = self._clock.time_msec()
|
|
|
|
|
|
|
|
keys_to_delete = set()
|
|
|
|
|
2018-09-21 09:52:21 -04:00
|
|
|
for key, cache_entry in iteritems(self._cache):
|
2015-02-16 11:16:47 -05:00
|
|
|
if now - cache_entry.time > self._expiry_ms:
|
|
|
|
keys_to_delete.add(key)
|
|
|
|
|
|
|
|
for k in keys_to_delete:
|
2017-01-17 06:18:13 -05:00
|
|
|
value = self._cache.pop(k)
|
|
|
|
if self.iterable:
|
2018-09-21 11:25:42 -04:00
|
|
|
self.metrics.inc_evictions(len(value.value))
|
|
|
|
else:
|
|
|
|
self.metrics.inc_evictions()
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
"[%s] _prune_cache before: %d, after len: %d",
|
2017-01-16 08:48:04 -05:00
|
|
|
self._cache_name, begin_length, len(self)
|
2015-02-16 11:16:47 -05:00
|
|
|
)
|
|
|
|
|
2016-02-23 06:54:48 -05:00
|
|
|
def __len__(self):
|
2017-01-16 08:48:04 -05:00
|
|
|
if self.iterable:
|
2018-09-21 11:25:42 -04:00
|
|
|
return sum(len(entry.value) for entry in itervalues(self._cache))
|
2017-01-16 08:48:04 -05:00
|
|
|
else:
|
|
|
|
return len(self._cache)
|
2016-02-23 06:54:48 -05:00
|
|
|
|
2015-02-16 11:16:47 -05:00
|
|
|
|
|
|
|
class _CacheEntry(object):
|
2018-09-21 09:52:21 -04:00
|
|
|
__slots__ = ["time", "value"]
|
|
|
|
|
2015-02-16 11:16:47 -05:00
|
|
|
def __init__(self, time, value):
|
|
|
|
self.time = time
|
|
|
|
self.value = value
|