Add cache eviction

This commit is contained in:
Matthias 2022-10-06 14:43:45 +00:00
parent 92a1d58df8
commit 7c702dd106
2 changed files with 17 additions and 0 deletions

View File

@ -1860,6 +1860,11 @@ class Exchange:
if min_date < self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0): if min_date < self._pairs_last_refresh_time.get((pair, timeframe, candle_type), 0):
# Cache can be used - do one-off call. # Cache can be used - do one-off call.
not_all_data = False not_all_data = False
else:
# Time jump detected, evict cache
logger.info(
f"Time jump detected. Evicting cache for {pair}, {timeframe}, {candle_type}")
del self._klines[(pair, timeframe, candle_type)]
if (not since_ms if (not since_ms
and (self._ft_has["ohlcv_require_since"] or not_all_data)): and (self._ft_has["ohlcv_require_since"] or not_all_data)):

View File

@ -2255,6 +2255,18 @@ def test_refresh_latest_ohlcv_cache(mocker, default_conf, candle_type, time_mach
assert len(res[pair1]) == 100 assert len(res[pair1]) == 100
assert len(res[pair2]) == 100 assert len(res[pair2]) == 100
# Move to distant future (so a 1 call would cause a hole in the data)
time_machine.move_to(start + timedelta(hours=2000))
ohlcv = generate_test_data_raw('1h', 100, start + timedelta(hours=1900))
exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv)
res = exchange.refresh_latest_ohlcv(pairs)
assert exchange._api_async.fetch_ohlcv.call_count == 2
assert len(res) == 2
# Cache eviction - new data.
assert len(res[pair1]) == 99
assert len(res[pair2]) == 99
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize("exchange_name", EXCHANGES) @pytest.mark.parametrize("exchange_name", EXCHANGES)