| Total Complexity | 8 |
| Total Lines | 75 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 2 | Data caching class |
||
| 3 | """ |
||
| 4 | |||
| 5 | __author__ = "Frederik Glücks" |
||
| 6 | __email__ = "[email protected]" |
||
| 7 | __copyright__ = "Frederik Glücks - Glücks GmbH" |
||
| 8 | |||
| 9 | |||
| 10 | class DataCaching: |
||
| 11 | """ |
||
| 12 | Data caching class |
||
| 13 | """ |
||
| 14 | __cache: dict = {} |
||
| 15 | |||
| 16 | @staticmethod |
||
| 17 | def clear() -> int: |
||
| 18 | """ |
||
| 19 | Complete clearing the data cache and return the number of cleared objects. |
||
| 20 | |||
| 21 | :return: int |
||
| 22 | """ |
||
| 23 | number_of_cached_entries = len(DataCaching.__cache) |
||
| 24 | DataCaching.__cache = {} |
||
| 25 | return number_of_cached_entries |
||
| 26 | |||
| 27 | @staticmethod |
||
| 28 | def get_statistic() -> dict: |
||
| 29 | """ |
||
| 30 | Return the number of cached object. Group by cache_group and object name |
||
| 31 | |||
| 32 | :return: dict |
||
| 33 | """ |
||
| 34 | statistic: dict = {} |
||
| 35 | |||
| 36 | for group_name in DataCaching.__cache: |
||
| 37 | statistic[group_name] = {} |
||
| 38 | |||
| 39 | for object_id in DataCaching.__cache[group_name]: |
||
| 40 | statistic[group_name][object_id] = len(DataCaching.__cache[group_name][object_id]) |
||
| 41 | |||
| 42 | return statistic |
||
| 43 | |||
| 44 | @staticmethod |
||
| 45 | def get(group_name: str, object_id: str) -> dict: |
||
| 46 | """ |
||
| 47 | Returns the value of object_id. |
||
| 48 | If the object doesn't exists an empty dict will returned |
||
| 49 | |||
| 50 | :param group_name: str |
||
| 51 | :param object_id: str |
||
| 52 | :return: dict |
||
| 53 | """ |
||
| 54 | |||
| 55 | if group_name not in DataCaching.__cache: |
||
| 56 | return {} |
||
| 57 | |||
| 58 | return DataCaching.__cache[group_name].get(object_id, {}) |
||
| 59 | |||
| 60 | @staticmethod |
||
| 61 | def set(group_name: str, object_id: str, value: dict): |
||
| 62 | """ |
||
| 63 | Adds the value of to the data cache |
||
| 64 | |||
| 65 | :param group_name: str |
||
| 66 | :param object_id: str |
||
| 67 | :param value: dict |
||
| 68 | :return: None |
||
| 69 | """ |
||
| 70 | |||
| 71 | if group_name not in DataCaching.__cache: |
||
| 72 | DataCaching.__cache[group_name] = {} |
||
| 73 | |||
| 74 | DataCaching.__cache[group_name][object_id] = value |
||
| 75 |