@@ -190,6 +190,10 @@ def get_cache_entry(self) -> Optional[P]:
190
190
return self .cache_entry ()
191
191
192
192
193
+ # Whether to insert new cache entries to the global list. We only add to it if
194
+ # time based eviction is enabled.
195
+ USE_GLOBAL_LIST = False
196
+
193
197
# A linked list of all cache entries, allowing efficient time based eviction.
194
198
GLOBAL_ROOT = _ListNode [_CacheEntry ]()
195
199
@@ -247,6 +251,9 @@ def expire_lru_cache_entries_after(hs: "HomeServer"):
247
251
"""Start a background job that expires all cache entries if they have not
248
252
been accessed for the given number of seconds.
249
253
"""
254
+ global USE_GLOBAL_LIST
255
+ USE_GLOBAL_LIST = True
256
+
250
257
clock = hs .get_clock ()
251
258
clock .looping_call (
252
259
_expire_old_entries , 30 * 1000 , clock , hs .config .caches .expiry_time_msec / 1000
@@ -275,7 +282,9 @@ def __init__(
275
282
):
276
283
self_ref = weakref .ref (self , lambda _ : self .drop_from_lists ())
277
284
self .list_node = _ListNode .insert_after (self_ref , root )
278
- self .global_list_node = _ListNode .insert_after (self_ref , GLOBAL_ROOT )
285
+ self .global_list_node = None
286
+ if USE_GLOBAL_LIST :
287
+ self .global_list_node = _ListNode .insert_after (self_ref , GLOBAL_ROOT )
279
288
280
289
# We store a weak reference to the cache object so that this _Node can
281
290
# remove itself from the cache. If the cache is dropped we ensure we
@@ -304,12 +313,14 @@ def __init__(
304
313
_get_size_of (key )
305
314
+ _get_size_of (value )
306
315
+ _get_size_of (self .list_node , recurse = False )
307
- + _get_size_of (self .global_list_node , recurse = False )
308
316
+ _get_size_of (self .callbacks , recurse = False )
309
317
+ _get_size_of (self , recurse = False )
310
318
)
311
319
self .memory += _get_size_of (self .memory , recurse = False )
312
320
321
+ if self .global_list_node :
322
+ self .memory += _get_size_of (self .global_list_node , recurse = False )
323
+
313
324
def add_callbacks (self , callbacks : Collection [Callable [[], None ]]) -> None :
314
325
"""Add to stored list of callbacks, removing duplicates."""
315
326
@@ -347,7 +358,9 @@ def drop_from_cache(self) -> None:
347
358
def drop_from_lists (self ) -> None :
348
359
"""Remove this node from the cache lists."""
349
360
self .list_node .remove_from_list ()
350
- self .global_list_node .remove_from_list ()
361
+
362
+ if self .global_list_node :
363
+ self .global_list_node .remove_from_list ()
351
364
352
365
353
366
class LruCache (Generic [KT , VT ]):
@@ -478,7 +491,8 @@ def add_node(key, value, callbacks: Collection[Callable[[], None]] = ()):
478
491
479
492
def move_node_to_front (node : _Node ):
480
493
node .list_node .move_after (list_root )
481
- node .global_list_node .move_after (GLOBAL_ROOT )
494
+ if node .global_list_node :
495
+ node .global_list_node .move_after (GLOBAL_ROOT )
482
496
483
497
def delete_node (node : _Node ) -> int :
484
498
node .drop_from_lists ()
0 commit comments