forked from Python-roborock/python-roborock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
71 lines (47 loc) · 2.03 KB
/
cache.py
File metadata and controls
71 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""This module provides caching functionality for the Roborock device management system.
This module defines a cache interface that you may use to cache device
information to avoid unnecessary API calls. Callers may implement
this interface to provide their own caching mechanism.
"""
from dataclasses import dataclass, field
from typing import Any, Protocol
from roborock.data import CombinedMapInfo, HomeData, NetworkInfo
from roborock.device_features import DeviceFeatures
@dataclass
class CacheData:
"""Data structure for caching device information."""
home_data: HomeData | None = None
"""Home data containing device and product information."""
network_info: dict[str, NetworkInfo] = field(default_factory=dict)
"""Network information indexed by device DUID."""
home_map_info: dict[int, CombinedMapInfo] = field(default_factory=dict)
"""Home map information indexed by map_flag."""
home_map_content: dict[int, bytes] = field(default_factory=dict)
"""Home cache content for each map data indexed 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."""
class Cache(Protocol):
"""Protocol for a cache that can store and retrieve values."""
async def get(self) -> CacheData:
"""Get cached value."""
...
async def set(self, value: CacheData) -> None:
"""Set value in the cache."""
...
class InMemoryCache(Cache):
"""In-memory cache implementation."""
def __init__(self) -> None:
"""Initialize the in-memory cache."""
self._data = CacheData()
async def get(self) -> CacheData:
return self._data
async def set(self, value: CacheData) -> None:
self._data = value
class NoCache(Cache):
"""No-op cache implementation."""
async def get(self) -> CacheData:
return CacheData()
async def set(self, value: CacheData) -> None:
pass