Skip to content

Commit

Permalink
feat: add dao layer for link as an example
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoPascoli committed Nov 26, 2024
1 parent fbc2094 commit dd40cf3
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions antarest/study/business/link/LinkFromCacheDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit dd40cf3

Please sign in to comment.