diff options
author | Erik Johnston <erikj@element.io> | 2024-02-13 13:24:11 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-13 13:24:11 +0000 |
commit | 7b4d7429f8e655fd5a1c3a65e6347577e8b49784 (patch) | |
tree | 3f56dc82682d97a9506e34b779ecd4e6d1edea20 /tests/util/test_lrucache.py | |
parent | Add a config to not send out device list updates for specific users (#16909) (diff) | |
download | synapse-7b4d7429f8e655fd5a1c3a65e6347577e8b49784.tar.xz |
Don't invalidate the entire event cache when we purge history (#16905)
We do this by adding support to the LRU cache for "extra indices" based on the cached value. This allows us to efficiently map from room ID to the cached events and only invalidate those.
Diffstat (limited to '')
-rw-r--r-- | tests/util/test_lrucache.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/util/test_lrucache.py b/tests/util/test_lrucache.py index dcc2b4be89..3f0d8139f8 100644 --- a/tests/util/test_lrucache.py +++ b/tests/util/test_lrucache.py @@ -383,3 +383,34 @@ class MemoryEvictionTestCase(unittest.HomeserverTestCase): # the items should still be in the cache self.assertEqual(cache.get("key1"), 1) self.assertEqual(cache.get("key2"), 2) + + +class ExtraIndexLruCacheTestCase(unittest.HomeserverTestCase): + def test_invalidate_simple(self) -> None: + cache: LruCache[str, int] = LruCache(10, extra_index_cb=lambda k, v: str(v)) + cache["key1"] = 1 + cache["key2"] = 2 + + cache.invalidate_on_extra_index("key1") + self.assertEqual(cache.get("key1"), 1) + self.assertEqual(cache.get("key2"), 2) + + cache.invalidate_on_extra_index("1") + self.assertEqual(cache.get("key1"), None) + self.assertEqual(cache.get("key2"), 2) + + def test_invalidate_multi(self) -> None: + cache: LruCache[str, int] = LruCache(10, extra_index_cb=lambda k, v: str(v)) + cache["key1"] = 1 + cache["key2"] = 1 + cache["key3"] = 2 + + cache.invalidate_on_extra_index("key1") + self.assertEqual(cache.get("key1"), 1) + self.assertEqual(cache.get("key2"), 1) + self.assertEqual(cache.get("key3"), 2) + + cache.invalidate_on_extra_index("1") + self.assertEqual(cache.get("key1"), None) + self.assertEqual(cache.get("key2"), None) + self.assertEqual(cache.get("key3"), 2) |