DivYonko commited on
Commit
8a6fade
Β·
1 Parent(s): c9f1020

fix: bust cache when store has data so UI shows messages

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -260,6 +260,8 @@ def start_scraper(slot_idx: int, video_id: str, redis_key: str) -> None:
260
  _SCRAPER_STOP[key] = stop_event
261
  _SCRAPER_THREADS[key] = t
262
  t.start()
 
 
263
 
264
 
265
  def stop_scraper(slot_idx: int) -> None:
@@ -518,9 +520,10 @@ def csv_download(df_export, label, filename):
518
  file_name=filename, mime="text/csv", key=filename)
519
 
520
 
521
- @st.cache_data(ttl=5, show_spinner=False)
522
- def load_stream_data(redis_key: str, limit: int | None = None):
523
- """Load and parse messages from the in-memory store. Cached for 5s."""
 
524
  if limit:
525
  raws = store_lrange(redis_key, -limit, -1)
526
  else:
@@ -888,7 +891,8 @@ st.divider()
888
  # ── DATA LOAD ─────────────────────────────────────────────────
889
  # Use stream A's redis_key (session state is the source of truth)
890
  _primary_key = st.session_state.streams[0]["redis_key"]
891
- all_data = load_stream_data(_primary_key)
 
892
  data = all_data[-msg_limit:] if len(all_data) > msg_limit else all_data
893
 
894
  if not all_data:
@@ -1410,7 +1414,7 @@ if len(active_streams) > 1:
1410
  sidx = st.session_state.streams.index(stream)
1411
  color = STREAM_COLORS[sidx]
1412
  slabel = STREAM_NAMES[sidx]
1413
- s_data = load_stream_data(stream["redis_key"])
1414
  if not s_data:
1415
  col.info(f"No data yet for Stream {slabel}")
1416
  continue
@@ -1439,7 +1443,7 @@ if len(active_streams) > 1:
1439
  sidx = st.session_state.streams.index(stream)
1440
  color = STREAM_COLORS[sidx]
1441
  slabel = STREAM_NAMES[sidx]
1442
- s_data = load_stream_data(stream["redis_key"])
1443
  if not s_data:
1444
  continue
1445
  s_df = pd.DataFrame(s_data)
 
260
  _SCRAPER_STOP[key] = stop_event
261
  _SCRAPER_THREADS[key] = t
262
  t.start()
263
+ # Clear the data cache so the UI picks up new messages immediately
264
+ load_stream_data.clear()
265
 
266
 
267
  def stop_scraper(slot_idx: int) -> None:
 
520
  file_name=filename, mime="text/csv", key=filename)
521
 
522
 
523
+ @st.cache_data(ttl=2, show_spinner=False)
524
+ def load_stream_data(redis_key: str, _store_len: int = 0, limit: int | None = None):
525
+ """Load and parse messages from the in-memory store. Cached for 2s.
526
+ _store_len is used as a cache-busting parameter β€” changes when new data arrives."""
527
  if limit:
528
  raws = store_lrange(redis_key, -limit, -1)
529
  else:
 
891
  # ── DATA LOAD ─────────────────────────────────────────────────
892
  # Use stream A's redis_key (session state is the source of truth)
893
  _primary_key = st.session_state.streams[0]["redis_key"]
894
+ _current_len = store_llen(_primary_key)
895
+ all_data = load_stream_data(_primary_key, _store_len=_current_len)
896
  data = all_data[-msg_limit:] if len(all_data) > msg_limit else all_data
897
 
898
  if not all_data:
 
1414
  sidx = st.session_state.streams.index(stream)
1415
  color = STREAM_COLORS[sidx]
1416
  slabel = STREAM_NAMES[sidx]
1417
+ s_data = load_stream_data(stream["redis_key"], _store_len=store_llen(stream["redis_key"]))
1418
  if not s_data:
1419
  col.info(f"No data yet for Stream {slabel}")
1420
  continue
 
1443
  sidx = st.session_state.streams.index(stream)
1444
  color = STREAM_COLORS[sidx]
1445
  slabel = STREAM_NAMES[sidx]
1446
+ s_data = load_stream_data(stream["redis_key"], _store_len=store_llen(stream["redis_key"]))
1447
  if not s_data:
1448
  continue
1449
  s_df = pd.DataFrame(s_data)