Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add support for 'iterable' to ExpiringCache
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jan 16, 2017
1 parent 0152129 commit 46aebbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
6 changes: 5 additions & 1 deletion synapse/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.1))


SIZE_OF_CACHE = int(1000 * CACHE_SIZE_FACTOR)
SIZE_OF_CACHE = int(10000 * CACHE_SIZE_FACTOR)
EVICTION_TIMEOUT_SECONDS = 60 * 60


Expand Down Expand Up @@ -77,6 +77,9 @@ def __init__(self, state, state_group, prev_group=None, delta_ids=None):
else:
self.state_id = _gen_state_id()

def __len__(self):
return len(self.state)


class StateHandler(object):
""" Responsible for doing state conflict resolution.
Expand All @@ -99,6 +102,7 @@ def start_caching(self):
clock=self.clock,
max_len=SIZE_OF_CACHE,
expiry_ms=EVICTION_TIMEOUT_SECONDS * 1000,
iterable=True,
reset_expiry_on_get=True,
)

Expand Down
26 changes: 17 additions & 9 deletions synapse/util/caches/expiringcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class ExpiringCache(object):
def __init__(self, cache_name, clock, max_len=0, expiry_ms=0,
reset_expiry_on_get=False):
reset_expiry_on_get=False, iterable=False):
"""
Args:
cache_name (str): Name of this cache, used for logging.
Expand All @@ -36,6 +36,8 @@ def __init__(self, cache_name, clock, max_len=0, expiry_ms=0,
evicted based on time.
reset_expiry_on_get (bool): If true, will reset the expiry time for
an item on access. Defaults to False.
iterable (bool): If true, the size is calculated by summing the
sizes of all entries, rather than the number of entries.
"""
self._cache_name = cache_name
Expand All @@ -49,7 +51,9 @@ def __init__(self, cache_name, clock, max_len=0, expiry_ms=0,

self._cache = {}

self.metrics = register_cache(cache_name, self._cache)
self.metrics = register_cache(cache_name, self)

self.iterable = iterable

def start(self):
if not self._expiry_ms:
Expand All @@ -66,14 +70,15 @@ def __setitem__(self, key, value):
self._cache[key] = _CacheEntry(now, value)

# Evict if there are now too many items
if self._max_len and len(self._cache.keys()) > self._max_len:
if self._max_len and len(self) > self._max_len:
sorted_entries = sorted(
self._cache.items(),
self._cache.keys(),
key=lambda item: item[1].time,
)

for k, _ in sorted_entries[self._max_len:]:
self._cache.pop(k)
while len(self) > self._max_len and sorted_entries:
key = sorted_entries.pop()
self._cache.pop(key)

def __getitem__(self, key):
try:
Expand All @@ -99,7 +104,7 @@ def _prune_cache(self):
# zero expiry time means don't expire. This should never get called
# since we have this check in start too.
return
begin_length = len(self._cache)
begin_length = len(self)

now = self._clock.time_msec()

Expand All @@ -114,11 +119,14 @@ def _prune_cache(self):

logger.debug(
"[%s] _prune_cache before: %d, after len: %d",
self._cache_name, begin_length, len(self._cache)
self._cache_name, begin_length, len(self)
)

def __len__(self):
return len(self._cache)
if self.iterable:
return sum(len(value.value) for value in self._cache.itervalues())
else:
return len(self._cache)


class _CacheEntry(object):
Expand Down

0 comments on commit 46aebbb

Please sign in to comment.