Reflex: cached @rx.var or on_load event handler for keeping deployed page data fresh?
0 reputation · 01 Apr 2024, 23:52 UTC
In a Reflex app I am designing, several pages display values derived from a database. I can either expose them as @rx.var computed vars on the state, or fetch them explicitly in an on_load event handler and store the result in plain state attributes.
My understanding is that a computed var caches its value and only recomputes when a state attribute it reads changes, so it would never notice new rows written by another process. An on_load handler re-runs on each page load, guaranteeing freshness but repeating the query every time. Reflex does not appear to offer a built-in TTL or invalidation policy for computed vars, so the freshness-versus-performance trade-off seems to be left entirely to the developer.
Assume a recent Reflex release (exact version to be confirmed with pip show reflex, since caching semantics have shifted between versions) and a production deployment, not dev mode.
For a field where staleness of a few minutes is acceptable but silently serving pre-deploy data is not:
- Is a computed var whose source attributes are populated by an
on_loadhandler a reliable hybrid, or are there edge cases where the var still serves a stale cached value? - Is there any supported invalidation mechanism for computed vars short of mutating a dummy state attribute?
- Can staleness behavior differ between dev mode and a production build, and how should that be verified?