Skip to content

Commit acd1119

Browse files
committed
Restructure code for caching modified params
1 parent db3c6ac commit acd1119

File tree

1 file changed

+30
-45
lines changed

1 file changed

+30
-45
lines changed

custom_components/studer_xcom/coordinator.py

+30-45
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def __init__(self, hass, config: dict[str,Any], options: dict[str,Any], is_temp=
246246
self._hass = hass
247247
self._store_key = self._install_id
248248
self._store = StuderCoordinatorStore(hass, self._store_key)
249-
self._cache = None
249+
self._cache = {}
250250
self._cache_last_write = datetime.min
251251

252252
# Diagnostics gathering
@@ -263,18 +263,14 @@ async def start(self) -> bool:
263263
self._entity_map_ts = datetime.now()
264264

265265
# Make sure our cache is available
266-
if self._cache is None:
267-
if self._store:
268-
_LOGGER.debug(f"Read persisted cache")
269-
store = await self._store.async_get_data() or {}
270-
self._cache = store.get("cache", {})
271-
else:
272-
self._cache = {}
266+
await self._async_read_cache()
273267

268+
# Start our Api
274269
return await self._api.start()
275270

276271

277272
async def stop(self):
273+
# Stop our Api
278274
await self._api.stop()
279275

280276

@@ -377,17 +373,7 @@ async def _async_update_data(self):
377373
await self._async_request_all_data()
378374

379375
# Periodically persist the cache
380-
if self._hass and \
381-
self._store and \
382-
self._cache and \
383-
(datetime.now() - self._cache_last_write).total_seconds() > CACHE_WRITE_PERIOD:
384-
385-
_LOGGER.debug(f"Persist cache")
386-
self._cache_last_write = datetime.now()
387-
388-
store = await self._store.async_get_data() or {}
389-
store["cache"] = self._cache
390-
await self._store.async_set_data(store)
376+
await self._async_persist_cache()
391377

392378
# return updated data
393379
return self._get_data()
@@ -449,26 +435,26 @@ async def async_modify_data(self, entity: StuderEntityData, value):
449435
return False
450436

451437

452-
async def _async_update_cache(self, context, data, force = False):
453-
"""
454-
Update the memory cache.
455-
Persisted cache is saved periodicaly by another function
456-
"""
457-
if self._cache:
458-
data["ts"] = datetime.now()
459-
self._cache[context] = data
460-
461-
462-
async def _async_fetch_from_cache(self, context):
463-
"""
464-
Fetch from the memory cache
465-
"""
466-
if self._cache:
467-
_LOGGER.debug(f"Fetch from cache: {context}")
468-
return self._cache.get(context, {})
438+
async def _async_read_cache(self):
439+
if self._store:
440+
_LOGGER.debug(f"Read persisted cache")
441+
store = await self._store.async_get_data() or {}
442+
self._cache = store.get("cache", {})
469443
else:
470-
return {}
471-
444+
_LOGGER.warning(f"Using empty cache; no store available to read persisted cache from")
445+
self._cache = {}
446+
447+
448+
async def _async_persist_cache(self):
449+
if self._store and (datetime.now() - self._cache_last_write).total_seconds() > CACHE_WRITE_PERIOD:
450+
451+
_LOGGER.debug(f"Persist cache")
452+
self._cache_last_write = datetime.now()
453+
454+
store = await self._store.async_get_data() or {}
455+
store["cache"] = self._cache
456+
await self._store.async_set_data(store)
457+
472458

473459
def _getModified(self, entity: StuderEntityData) -> Any:
474460
"""
@@ -483,15 +469,14 @@ async def _addModified(self, entity: StuderEntityData, value: Any):
483469
"""
484470
Remember a modified params value. Persist it in cache.
485471
"""
486-
if self._cache:
487-
modified_params = self._cache.get(MODIFIED_PARAMS, {})
488-
modified_params[entity.object_id] = value
472+
modified_params = self._cache.get(MODIFIED_PARAMS, {})
473+
modified_params[entity.object_id] = value
489474

490-
self._cache[MODIFIED_PARAMS] = modified_params
491-
self._cache[MODIFIED_PARAMS_TS] = datetime.now()
475+
self._cache[MODIFIED_PARAMS] = modified_params
476+
self._cache[MODIFIED_PARAMS_TS] = datetime.now()
492477

493-
# Trigger write of cache
494-
self._cache_last_write = datetime.min
478+
# Trigger write of cache
479+
self._cache_last_write = datetime.min
495480

496481

497482
async def _addDiagnostic(self, diag_key: str, success: bool, e: Exception|None = None):

0 commit comments

Comments
 (0)