diff options
author | Paul "LeoNerd" Evans <paul@matrix.org> | 2015-02-23 18:41:58 +0000 |
---|---|---|
committer | Paul "LeoNerd" Evans <paul@matrix.org> | 2015-02-23 18:41:58 +0000 |
commit | 9640510de206d673e4c0c9cbd1cef219bcd488b2 (patch) | |
tree | 1f6c068901634d7f24380ca2bf617e731c33a63d /synapse/storage | |
parent | Use cache.pop() instead of a separate membership test + del [] (diff) | |
download | synapse-9640510de206d673e4c0c9cbd1cef219bcd488b2.tar.xz |
Use OrderedDict for @cached backing store, so we can evict the oldest key unbiased
Diffstat (limited to 'synapse/storage')
-rw-r--r-- | synapse/storage/_base.py | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py index da698cb3b8..c98dd36aed 100644 --- a/synapse/storage/_base.py +++ b/synapse/storage/_base.py @@ -23,7 +23,7 @@ from synapse.util.lrucache import LruCache from twisted.internet import defer -import collections +from collections import namedtuple, OrderedDict import simplejson as json import sys import time @@ -54,14 +54,11 @@ def cached(max_entries=1000): calling the calculation function. """ def wrap(orig): - cache = {} + cache = OrderedDict() def prefill(key, value): while len(cache) > max_entries: - # TODO(paul): This feels too biased. However, a random index - # would be a bit inefficient, walking the list of keys just - # to ignore most of them? - del cache[cache.keys()[0]] + cache.popitem(last=False) cache[key] = value @@ -836,7 +833,7 @@ class JoinHelper(object): for table in self.tables: res += [f for f in table.fields if f not in res] - self.EntryType = collections.namedtuple("JoinHelperEntry", res) + self.EntryType = namedtuple("JoinHelperEntry", res) def get_fields(self, **prefixes): """Get a string representing a list of fields for use in SELECT |