Skip to content

Commit 4543702

Browse files
committed
Fix caching
1 parent a025c94 commit 4543702

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

kadi/commands/commands_v2.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ def get_cmds(
257257
# Get current "now" time, which might be mocked via CXOTIME_NOW.
258258
# get_cxotime_now() returns None if the env var is not set.
259259
cxotime_now = get_cxotime_now()
260-
now = CxoTime(cxotime_now)
261260

262261
# Cache key used for CMDS_RECENT and MATCHING_BLOCKS. These are all the relevant
263262
# kwargs in update_archive_and_get_cmds_recent().
264263
cache_key = scenario, cxotime_now, lookback, event_filter
264+
logger.info(f"Cache key: {cache_key}")
265265

266266
# For flight scenario or no internet or if the query stop time is guaranteed
267267
# to not require recent commands then just use the archive.
268-
before_recent_cmds = stop < now - lookback * u.day
268+
before_recent_cmds = stop < CxoTime(cxotime_now) - lookback * u.day
269269
if scenario == "flight" or not HAS_INTERNET or before_recent_cmds:
270270
cmds = IDX_CMDS
271271
logger.info(
@@ -274,16 +274,21 @@ def get_cmds(
274274
)
275275
else:
276276
if cache_key not in CMDS_RECENT:
277+
logger.info(
278+
"Recent commands not in cache: updating local cmd_events, loads "
279+
"and getting recent commands"
280+
)
277281
cmds_recent = update_cmd_events_and_loads_and_get_cmds_recent(
278282
scenario,
279283
lookback=lookback,
280284
stop_loads=cxotime_now,
281-
cache=True,
282285
pars_dict=PARS_DICT,
283286
rev_pars_dict=REV_PARS_DICT,
284287
event_filter=event_filter,
285288
)
289+
CMDS_RECENT[cache_key] = cmds_recent
286290
else:
291+
logger.info("Getting recent commands from cache")
287292
cmds_recent = CMDS_RECENT[cache_key]
288293

289294
# Get `cmds` as correct mix of recent and archive commands that contains
@@ -361,7 +366,6 @@ def update_cmd_events_and_loads_and_get_cmds_recent(
361366
*,
362367
lookback=None,
363368
stop_loads=None,
364-
cache=True,
365369
pars_dict=None,
366370
rev_pars_dict=None,
367371
event_filter: Callable | list[Callable] | None = None,
@@ -536,10 +540,6 @@ def update_cmd_events_and_loads_and_get_cmds_recent(
536540
cmds_recent = add_obs_cmds(cmds_recent, pars_dict, rev_pars_dict)
537541
cmds_recent.meta["loads_start"] = start_cmds.date
538542

539-
if cache:
540-
# Cache recent commands so future requests for the same scenario are fast
541-
CMDS_RECENT[scenario] = cmds_recent
542-
543543
return cmds_recent
544544

545545

@@ -1428,7 +1428,6 @@ def _update_cmds_archive(lookback, stop_loads, match_prev_cmds, scenario, data_r
14281428
scenario=scenario,
14291429
stop_loads=stop_loads,
14301430
lookback=lookback,
1431-
cache=False,
14321431
pars_dict=pars_dict,
14331432
)
14341433

kadi/commands/observations.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,9 @@ def get_observations(
665665
list of dict
666666
Observation parameters for matching observations.
667667
"""
668+
from kadi.commands import conf
668669
from kadi.commands.commands_v2 import get_cmds
670+
from kadi.commands.core import get_cxotime_now
669671

670672
if starcat_date is not None:
671673
start = starcat_date if start is None else start
@@ -674,7 +676,8 @@ def get_observations(
674676
stop = (CxoTime.now() + 1 * u.year) if stop is None else CxoTime(stop)
675677

676678
if cmds is None:
677-
if scenario not in OBSERVATIONS:
679+
cache_key = scenario, get_cxotime_now(), conf.default_lookback, event_filter
680+
if cache_key not in OBSERVATIONS:
678681
cmds = get_cmds(scenario=scenario, event_filter=event_filter)
679682
cmds_obs = cmds[cmds["tlmsid"] == "OBS"]
680683
obsids = []
@@ -686,9 +689,9 @@ def get_observations(
686689
obsids.append(_obsid)
687690

688691
cmds_obs["obsid"] = obsids
689-
OBSERVATIONS[scenario] = cmds_obs
692+
OBSERVATIONS[cache_key] = cmds_obs
690693
else:
691-
cmds_obs = OBSERVATIONS[scenario]
694+
cmds_obs = OBSERVATIONS[cache_key]
692695
else:
693696
cmds_obs = cmds[cmds["tlmsid"] == "OBS"]
694697

0 commit comments

Comments
 (0)