feat(midea): serve cloud AC state from cache (stale-while-revalidate) - #215
Merged
Conversation
/midea and the portal widget called getState per device on every load, and getState always did a full cloud round-trip (~1.4s) — plus a re-login whenever Midea had expired the token. The in-memory state cache was written but never read to short-circuit. Now getState serves a known-online cached cloud state instantly within a 90s TTL (no cloud round-trip, no re-login); beyond the TTL it still returns the cached state immediately but kicks a single deduped background refresh. LAN devices are unchanged. Session persistence already worked and is untouched; this just stops hitting the cloud (and thus its token expiry) on every load.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
/mideaund das Portal-Widget rufen beim Laden pro GerätgetState()auf, undgetStatemachte für Cloud-Geräte immer einen vollen Cloud-Roundtrip (~1,4 s) — plus einen Re-Login, sobald Midea den Token hat ablaufen lassen. Der vorhandene In-Memory-State-Cache wurde geschrieben, aber nie zum Ausliefern gelesen. Ergebnis: langsames Widget-Laden und der Eindruck „bei jedem Aufruf ein Login".Diagnose (live am Prod-Container verifiziert, read-only)
accessToken+ Session-Keys) ist verschlüsselt persistiert und wiederverwendbar —listDevicesundsendCommand(buildQuery)funktionieren mit der gespeicherten Session ohne Re-Login.Fix
getStateliefert einen bekannt-online Cloud-Zustand sofort aus dem Cache aus, solange er < 90 s alt ist (kein Cloud-Roundtrip, kein Re-Login). Darüber hinaus wird der gecachte Zustand weiterhin sofort zurückgegeben, aber ein entprellter Hintergrund-Refresh angestoßen. LAN-Geräte unverändert. Session-Persistenz war bereits vorhanden und bleibt unangetastet.CLOUD_STATE_TTL_MS = 90000, per-Device Dedupe der Hintergrund-Refreshes.setState) aktualisieren den Cache ohnehin sofort → Anzeige bleibt live.Tests
midea_devices.test.js: kalt = genau 1 Cloud-Roundtrip, warm innerhalb TTL = kein weiterer Call.midea_devices,midea_api,midea_portal_display(47 Tests).🤖 Generated with Claude Code