mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-05 14:44:55 -04:00
Add support for evicting cache entries based on last access time. (#10205)
This commit is contained in:
parent
10671da05b
commit
7a5873277e
9 changed files with 483 additions and 93 deletions
|
@ -15,7 +15,7 @@
|
|||
|
||||
from unittest.mock import Mock
|
||||
|
||||
from synapse.util.caches.lrucache import LruCache
|
||||
from synapse.util.caches.lrucache import LruCache, setup_expire_lru_cache_entries
|
||||
from synapse.util.caches.treecache import TreeCache
|
||||
|
||||
from tests import unittest
|
||||
|
@ -260,3 +260,47 @@ class LruCacheSizedTestCase(unittest.HomeserverTestCase):
|
|||
self.assertEquals(cache["key3"], [3])
|
||||
self.assertEquals(cache["key4"], [4])
|
||||
self.assertEquals(cache["key5"], [5, 6])
|
||||
|
||||
|
||||
class TimeEvictionTestCase(unittest.HomeserverTestCase):
|
||||
"""Test that time based eviction works correctly."""
|
||||
|
||||
def default_config(self):
|
||||
config = super().default_config()
|
||||
|
||||
config.setdefault("caches", {})["expiry_time"] = "30m"
|
||||
|
||||
return config
|
||||
|
||||
def test_evict(self):
|
||||
setup_expire_lru_cache_entries(self.hs)
|
||||
|
||||
cache = LruCache(5, clock=self.hs.get_clock())
|
||||
|
||||
# Check that we evict entries we haven't accessed for 30 minutes.
|
||||
cache["key1"] = 1
|
||||
cache["key2"] = 2
|
||||
|
||||
self.reactor.advance(20 * 60)
|
||||
|
||||
self.assertEqual(cache.get("key1"), 1)
|
||||
|
||||
self.reactor.advance(20 * 60)
|
||||
|
||||
# We have only touched `key1` in the last 30m, so we expect that to
|
||||
# still be in the cache while `key2` should have been evicted.
|
||||
self.assertEqual(cache.get("key1"), 1)
|
||||
self.assertEqual(cache.get("key2"), None)
|
||||
|
||||
# Check that re-adding an expired key works correctly.
|
||||
cache["key2"] = 3
|
||||
self.assertEqual(cache.get("key2"), 3)
|
||||
|
||||
self.reactor.advance(20 * 60)
|
||||
|
||||
self.assertEqual(cache.get("key2"), 3)
|
||||
|
||||
self.reactor.advance(20 * 60)
|
||||
|
||||
self.assertEqual(cache.get("key1"), None)
|
||||
self.assertEqual(cache.get("key2"), 3)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue