From dd40cf30e6c7d6af6c9cc424d86c71fcdbe35020 Mon Sep 17 00:00:00 2001 From: Theo Pascoli Date: Tue, 26 Nov 2024 12:34:42 +0100 Subject: [PATCH] feat: add dao layer for link as an example --- antarest/study/business/link/LinkFromCacheDAO.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/antarest/study/business/link/LinkFromCacheDAO.py b/antarest/study/business/link/LinkFromCacheDAO.py index 7b44756d9b..1f8af08ebe 100644 --- a/antarest/study/business/link/LinkFromCacheDAO.py +++ b/antarest/study/business/link/LinkFromCacheDAO.py @@ -103,25 +103,21 @@ def create_link(self, study: Study, link_dto: LinkDTO) -> LinkDTO: cache_key = self._get_cache_key(study.id) cached_links = self.redis.get(cache_key) - # Deserialize or initialize - if isinstance(cached_links, str): # If it's a JSON string + if isinstance(cached_links, str): try: - cached_links = json.loads(cached_links) # Deserialize to a Python dict + cached_links = json.loads(cached_links) except json.JSONDecodeError: - cached_links = {"links": []} # Initialize an empty dictionary - elif not isinstance(cached_links, dict): # If not a dict, initialize + cached_links = {"links": []} + elif not isinstance(cached_links, dict): cached_links = {"links": []} - # Access the list of links in cached_links links_data = cached_links.get("links", []) - if not isinstance(links_data, list): # Ensure it's a list + if not isinstance(links_data, list): links_data = [] - # Add the new link link_data = link_dto.model_dump(by_alias=True, exclude_unset=True) links_data.append(link_data) - # Update the cache cached_links["links"] = links_data self.redis.put(cache_key, cached_links)