-
Notifications
You must be signed in to change notification settings - Fork 57
fix: Fix issues with the cache clobbering information for each device #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a179410
d64f3ed
765447b
2424f72
617b329
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,33 +12,71 @@ | |
| from roborock.device_features import DeviceFeatures | ||
|
|
||
|
|
||
| @dataclass | ||
| class DeviceCacheData(RoborockBase): | ||
| """Data structure for caching device information.""" | ||
|
|
||
| network_info: NetworkInfo | None = None | ||
| """Network information for the device""" | ||
|
|
||
| home_map_info: dict[int, CombinedMapInfo] | None = None | ||
| """Home map information for the device by map_flag.""" | ||
|
|
||
| home_map_content_base64: dict[int, str] | None = None | ||
| """Home cache content for the device (encoded base64) by map_flag.""" | ||
|
|
||
| device_features: DeviceFeatures | None = None | ||
| """Device features information.""" | ||
|
|
||
| trait_data: dict[str, Any] | None = None | ||
| """Trait-specific cached data used internally for caching device features.""" | ||
|
|
||
|
|
||
| @dataclass | ||
| class CacheData(RoborockBase): | ||
| """Data structure for caching device information.""" | ||
|
|
||
| home_data: HomeData | None = None | ||
| """Home data containing device and product information.""" | ||
|
|
||
| device_info: dict[str, DeviceCacheData] = field(default_factory=dict) | ||
| """Per-device cached information indexed by device DUID.""" | ||
|
|
||
| network_info: dict[str, NetworkInfo] = field(default_factory=dict) | ||
| """Network information indexed by device DUID.""" | ||
| """Network information indexed by device DUID. | ||
|
|
||
| This is deprecated. Use the per-device `network_info` field instead. | ||
| """ | ||
|
|
||
| home_map_info: dict[int, CombinedMapInfo] = field(default_factory=dict) | ||
| """Home map information indexed by map_flag.""" | ||
| """Home map information indexed by map_flag. | ||
|
|
||
| This is deprecated. Use the per-device `home_map_info` field instead. | ||
| """ | ||
|
|
||
| home_map_content: dict[int, bytes] = field(default_factory=dict) | ||
| """Home cache content for each map data indexed by map_flag. | ||
|
|
||
| This is deprecated in favor of `home_map_content_base64`. | ||
| This is deprecated. Use the per-device `home_map_content_base64` field instead. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to hold onto the deprecated value anymore?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i want to just let it die and we can drop the old field name in the next major release as a breaking change. |
||
| """ | ||
|
|
||
| home_map_content_base64: dict[int, str] = field(default_factory=dict) | ||
| """Home cache content for each map data (encoded base64) indexed by map_flag.""" | ||
| """Home cache content for each map data (encoded base64) indexed by map_flag. | ||
|
|
||
| This is deprecated. Use the per-device `home_map_content_base64` field instead. | ||
| """ | ||
|
|
||
| device_features: DeviceFeatures | None = None | ||
| """Device features information.""" | ||
| """Device features information. | ||
|
|
||
| This is deprecated. Use the per-device `device_features` field instead. | ||
| """ | ||
|
|
||
| trait_data: dict[str, Any] | None = None | ||
| """Trait-specific cached data used internally for caching device features.""" | ||
| """Trait-specific cached data used internally for caching device features. | ||
|
|
||
| This is deprecated. Use the per-device `trait_data` field instead. | ||
| """ | ||
|
|
||
|
|
||
| class Cache(Protocol): | ||
|
|
@@ -53,6 +91,34 @@ async def set(self, value: CacheData) -> None: | |
| ... | ||
|
|
||
|
|
||
| @dataclass | ||
| class DeviceCache(RoborockBase): | ||
| """Provides a cache interface for a specific device. | ||
|
|
||
| This is a convenience wrapper around a general Cache implementation to | ||
| provide device-specific caching functionality. | ||
| """ | ||
|
|
||
| def __init__(self, duid: str, cache: Cache) -> None: | ||
| """Initialize the device cache with the given cache implementation.""" | ||
| self._duid = duid | ||
| self._cache = cache | ||
|
|
||
| async def get(self) -> DeviceCacheData: | ||
| """Get cached device-specific information.""" | ||
| cache_data = await self._cache.get() | ||
| if self._duid not in cache_data.device_info: | ||
| cache_data.device_info[self._duid] = DeviceCacheData() | ||
| await self._cache.set(cache_data) | ||
| return cache_data.device_info[self._duid] | ||
|
|
||
| async def set(self, device_cache_data: DeviceCacheData) -> None: | ||
| """Set cached device-specific information.""" | ||
| cache_data = await self._cache.get() | ||
| cache_data.device_info[self._duid] = device_cache_data | ||
| await self._cache.set(cache_data) | ||
|
|
||
|
|
||
| class InMemoryCache(Cache): | ||
| """In-memory cache implementation.""" | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.