Implement the main getEvent cache using Cache() instead of a custom application of LruCache; also unify its two-level structure into just one

This commit is contained in:
Paul "LeoNerd" Evans 2015-03-25 19:12:16 +00:00
parent f173d40a32
commit 953e40f9dc

View file

@ -241,10 +241,8 @@ class SQLBaseStore(object):
self._txn_perf_counters = PerformanceCounters() self._txn_perf_counters = PerformanceCounters()
self._get_event_counters = PerformanceCounters() self._get_event_counters = PerformanceCounters()
self._get_event_cache = LruCache(hs.config.event_cache_size) self._get_event_cache = Cache("*getEvent*", keylen=3, lru=True,
max_entries=hs.config.event_cache_size)
# Pretend the getEventCache is just another named cache
caches_by_name["*getEvent*"] = self._get_event_cache
def start_profiling(self): def start_profiling(self):
self._previous_loop_ts = self._clock.time_msec() self._previous_loop_ts = self._clock.time_msec()
@ -733,7 +731,9 @@ class SQLBaseStore(object):
return [e for e in events if e] return [e for e in events if e]
def _invalidate_get_event_cache(self, event_id): def _invalidate_get_event_cache(self, event_id):
self._get_event_cache.pop(event_id) for check_redacted in (False, True):
for get_prev_content in (False, True):
self._get_event_cache.invalidate(event_id, check_redacted, get_prev_content)
def _get_event_txn(self, txn, event_id, check_redacted=True, def _get_event_txn(self, txn, event_id, check_redacted=True,
get_prev_content=False, allow_rejected=False): get_prev_content=False, allow_rejected=False):
@ -745,19 +745,14 @@ class SQLBaseStore(object):
sql_getevents_timer.inc_by(curr_time - last_time, desc) sql_getevents_timer.inc_by(curr_time - last_time, desc)
return curr_time return curr_time
cache = self._get_event_cache.setdefault(event_id, {})
try: try:
# Separate cache entries for each way to invoke _get_event_txn ret = self._get_event_cache.get(event_id, check_redacted, get_prev_content)
ret = cache[(check_redacted, get_prev_content)]
cache_counter.inc_hits("*getEvent*")
if allow_rejected or not ret.rejected_reason: if allow_rejected or not ret.rejected_reason:
return ret return ret
else: else:
return None return None
except KeyError: except KeyError:
cache_counter.inc_misses("*getEvent*")
pass pass
finally: finally:
start_time = update_counter("event_cache", start_time) start_time = update_counter("event_cache", start_time)
@ -788,7 +783,7 @@ class SQLBaseStore(object):
get_prev_content=get_prev_content, get_prev_content=get_prev_content,
rejected_reason=rejected_reason, rejected_reason=rejected_reason,
) )
cache[(check_redacted, get_prev_content)] = result self._get_event_cache.prefill(event_id, check_redacted, get_prev_content, result)
if allow_rejected or not rejected_reason: if allow_rejected or not rejected_reason:
return result return result