Flush in-memory cache to session after a limit is reached

Should fully close #3989.
Should help with #3235.
This commit is contained in:
Lonami Exo
2023-04-06 13:45:12 +02:00
parent cb04e269c0
commit 97b0ba6707
3 changed files with 42 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import time
import traceback
import typing
import logging
import warnings
from collections import deque
from .. import events, utils, errors
@@ -281,6 +282,24 @@ class UpdateMethods:
continue
if len(self._mb_entity_cache) >= self._entity_cache_limit:
self._log[__name__].info(
'In-memory entity cache limit reached (%s/%s), flushing to session',
len(self._mb_entity_cache),
self._entity_cache_limit
)
self._save_states_and_entities()
self._mb_entity_cache.retain(lambda id: id == self._mb_entity_cache.self_id or id in self._message_box.map)
if len(self._mb_entity_cache) >= self._entity_cache_limit:
warnings.warn('in-memory entities exceed entity_cache_limit after flushing; consider setting a larger limit')
self._log[__name__].info(
'In-memory entity cache at %s/%s after flushing to session',
len(self._mb_entity_cache),
self._entity_cache_limit
)
get_diff = self._message_box.get_difference()
if get_diff:
self._log[__name__].debug('Getting difference for account updates')