summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2016-01-22 12:18:14 +0000
committerDavid Baker <dave@matrix.org>2016-01-22 12:18:14 +0000
commitd552861346d6f2f3d50fa0aff3e239d17cf9b7c0 (patch)
tree4cd93b237f60276f5a9735c365b5d80c3f1df47e
parentMake LRU cache not default to treecache & add options to use it (diff)
downloadsynapse-d552861346d6f2f3d50fa0aff3e239d17cf9b7c0.tar.xz
Revert all the bits changing keys of eeverything that used LRUCaches to tuples
-rw-r--r--synapse/push/push_rule_evaluator.py6
-rw-r--r--synapse/util/caches/dictionary_cache.py10
-rw-r--r--synapse/util/caches/lrucache.py2
-rw-r--r--tests/storage/test__base.py26
-rw-r--r--tests/util/test_lrucache.py44
5 files changed, 44 insertions, 44 deletions
diff --git a/synapse/push/push_rule_evaluator.py b/synapse/push/push_rule_evaluator.py
index 27b0de4f66..dca018af95 100644
--- a/synapse/push/push_rule_evaluator.py
+++ b/synapse/push/push_rule_evaluator.py
@@ -309,14 +309,14 @@ def _flatten_dict(d, prefix=[], result={}):
     return result
 
 
-regex_cache = LruCache(5000, 1)
+regex_cache = LruCache(5000)
 
 
 def _compile_regex(regex_str):
-    r = regex_cache.get((regex_str,), None)
+    r = regex_cache.get(regex_str, None)
     if r:
         return r
 
     r = re.compile(regex_str, flags=re.IGNORECASE)
-    regex_cache[(regex_str,)] = r
+    regex_cache[regex_str] = r
     return r
diff --git a/synapse/util/caches/dictionary_cache.py b/synapse/util/caches/dictionary_cache.py
index b7964467eb..f92d80542b 100644
--- a/synapse/util/caches/dictionary_cache.py
+++ b/synapse/util/caches/dictionary_cache.py
@@ -32,7 +32,7 @@ class DictionaryCache(object):
     """
 
     def __init__(self, name, max_entries=1000):
-        self.cache = LruCache(max_size=max_entries, keylen=1)
+        self.cache = LruCache(max_size=max_entries)
 
         self.name = name
         self.sequence = 0
@@ -56,7 +56,7 @@ class DictionaryCache(object):
                 )
 
     def get(self, key, dict_keys=None):
-        entry = self.cache.get((key,), self.sentinel)
+        entry = self.cache.get(key, self.sentinel)
         if entry is not self.sentinel:
             cache_counter.inc_hits(self.name)
 
@@ -78,7 +78,7 @@ class DictionaryCache(object):
         # Increment the sequence number so that any SELECT statements that
         # raced with the INSERT don't update the cache (SYN-369)
         self.sequence += 1
-        self.cache.pop((key,), None)
+        self.cache.pop(key, None)
 
     def invalidate_all(self):
         self.check_thread()
@@ -96,8 +96,8 @@ class DictionaryCache(object):
                 self._update_or_insert(key, value)
 
     def _update_or_insert(self, key, value):
-        entry = self.cache.setdefault((key,), DictionaryEntry(False, {}))
+        entry = self.cache.setdefault(key, DictionaryEntry(False, {}))
         entry.value.update(value)
 
     def _insert(self, key, value):
-        self.cache[(key,)] = DictionaryEntry(True, value)
+        self.cache[key] = DictionaryEntry(True, value)
diff --git a/synapse/util/caches/lrucache.py b/synapse/util/caches/lrucache.py
index 23e86ec110..5f9405c95f 100644
--- a/synapse/util/caches/lrucache.py
+++ b/synapse/util/caches/lrucache.py
@@ -29,7 +29,7 @@ def enumerate_leaves(node, depth):
 
 class LruCache(object):
     """Least-recently-used cache."""
-    def __init__(self, max_size, keylen, cache_type=dict):
+    def __init__(self, max_size, keylen=1, cache_type=dict):
         cache = cache_type()
         self.size = 0
         list_root = []
diff --git a/tests/storage/test__base.py b/tests/storage/test__base.py
index c4e4c9b4bf..219288621d 100644
--- a/tests/storage/test__base.py
+++ b/tests/storage/test__base.py
@@ -56,42 +56,42 @@ class CacheTestCase(unittest.TestCase):
     def test_eviction(self):
         cache = Cache("test", max_entries=2)
 
-        cache.prefill((1,), "one")
-        cache.prefill((2,), "two")
-        cache.prefill((3,), "three")  # 1 will be evicted
+        cache.prefill(1, "one")
+        cache.prefill(2, "two")
+        cache.prefill(3, "three")  # 1 will be evicted
 
         failed = False
         try:
-            cache.get((1,))
+            cache.get(1)
         except KeyError:
             failed = True
 
         self.assertTrue(failed)
 
-        cache.get((2,))
-        cache.get((3,))
+        cache.get(2)
+        cache.get(3)
 
     def test_eviction_lru(self):
         cache = Cache("test", max_entries=2, lru=True)
 
-        cache.prefill((1,), "one")
-        cache.prefill((2,), "two")
+        cache.prefill(1, "one")
+        cache.prefill(2, "two")
 
         # Now access 1 again, thus causing 2 to be least-recently used
-        cache.get((1,))
+        cache.get(1)
 
-        cache.prefill((3,), "three")
+        cache.prefill(3, "three")
 
         failed = False
         try:
-            cache.get((2,))
+            cache.get(2)
         except KeyError:
             failed = True
 
         self.assertTrue(failed)
 
-        cache.get((1,))
-        cache.get((3,))
+        cache.get(1)
+        cache.get(3)
 
 
 class CacheDecoratorTestCase(unittest.TestCase):
diff --git a/tests/util/test_lrucache.py b/tests/util/test_lrucache.py
index bcad1d4258..2cd3d26454 100644
--- a/tests/util/test_lrucache.py
+++ b/tests/util/test_lrucache.py
@@ -22,37 +22,37 @@ from synapse.util.caches.treecache import TreeCache
 class LruCacheTestCase(unittest.TestCase):
 
     def test_get_set(self):
-        cache = LruCache(1, 1)
-        cache[("key",)] = "value"
-        self.assertEquals(cache.get(("key",)), "value")
-        self.assertEquals(cache[("key",)], "value")
+        cache = LruCache(1)
+        cache["key"] = "value"
+        self.assertEquals(cache.get("key"), "value")
+        self.assertEquals(cache["key"], "value")
 
     def test_eviction(self):
-        cache = LruCache(2, 1)
-        cache[(1,)] = 1
-        cache[(2,)] = 2
+        cache = LruCache(2)
+        cache[1] = 1
+        cache[2] = 2
 
-        self.assertEquals(cache.get((1,)), 1)
-        self.assertEquals(cache.get((2,)), 2)
+        self.assertEquals(cache.get(1), 1)
+        self.assertEquals(cache.get(2), 2)
 
-        cache[(3,)] = 3
+        cache[3] = 3
 
-        self.assertEquals(cache.get((1,)), None)
-        self.assertEquals(cache.get((2,)), 2)
-        self.assertEquals(cache.get((3,)), 3)
+        self.assertEquals(cache.get(1), None)
+        self.assertEquals(cache.get(2), 2)
+        self.assertEquals(cache.get(3), 3)
 
     def test_setdefault(self):
-        cache = LruCache(1, 1)
-        self.assertEquals(cache.setdefault(("key",), 1), 1)
-        self.assertEquals(cache.get(("key",)), 1)
-        self.assertEquals(cache.setdefault(("key",), 2), 1)
-        self.assertEquals(cache.get(("key",)), 1)
+        cache = LruCache(1)
+        self.assertEquals(cache.setdefault("key", 1), 1)
+        self.assertEquals(cache.get("key"), 1)
+        self.assertEquals(cache.setdefault("key", 2), 1)
+        self.assertEquals(cache.get("key"), 1)
 
     def test_pop(self):
-        cache = LruCache(1, 1)
-        cache[("key",)] = 1
-        self.assertEquals(cache.pop(("key",)), 1)
-        self.assertEquals(cache.pop(("key",)), None)
+        cache = LruCache(1)
+        cache["key"] = 1
+        self.assertEquals(cache.pop("key"), 1)
+        self.assertEquals(cache.pop("key"), None)
 
     def test_del_multi(self):
         cache = LruCache(4, 2, cache_type=TreeCache)