-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8df7e9
commit 04727b3
Showing
10 changed files
with
288 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
|
||
from antarest.core.interfaces.cache import ICache | ||
from antarest.study.dao.link.composite_link_dao import CompositeLinkDAO | ||
from antarest.study.storage.storage_service import StudyStorageService | ||
|
||
|
||
class DAOFactory: | ||
def __init__(self, storage_service: StudyStorageService, cache_service: ICache): | ||
self.storage_service = storage_service | ||
self.cache_service = cache_service | ||
|
||
def create_link_dao(self) -> CompositeLinkDAO: | ||
return CompositeLinkDAO(self.storage_service, self.cache_service) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
|
||
import typing as t | ||
|
||
from antarest.core.interfaces.cache import ICache | ||
from antarest.study.business.model.link_model import LinkDTO | ||
|
||
|
||
class CacheLinkDao: | ||
def __init__(self, cache: ICache): | ||
self.cache = cache | ||
|
||
def get_links(self, study_id: str) -> t.Optional[t.List[LinkDTO]]: | ||
cache_key = self._get_cache_key(study_id) | ||
cached_data = self.cache.get(cache_key) | ||
if cached_data is None: | ||
return None | ||
return [LinkDTO.model_validate(link) for link in cached_data["links"]] | ||
|
||
def put(self, study_id: str, link: LinkDTO, timeout: int = 600000) -> None: | ||
try: | ||
cache_key = self._get_cache_key(study_id) | ||
cached_links = self.cache.get(cache_key) | ||
|
||
if cached_links is None: | ||
cached_links = {"links": []} | ||
|
||
existing_links = cached_links.get("links", []) | ||
|
||
if isinstance(existing_links, dict): | ||
existing_links = list(existing_links.values()) | ||
elif not isinstance(existing_links, list): | ||
existing_links = [] | ||
|
||
link_data = link.model_dump() | ||
existing_links.append(link_data) | ||
|
||
cached_links["links"] = existing_links | ||
self.cache.put(cache_key, cached_links, timeout) | ||
except: | ||
raise | ||
|
||
def invalidate(self, study_id: str) -> None: | ||
cache_key = self._get_cache_key(study_id) | ||
self.cache.invalidate(cache_key) | ||
|
||
def _get_cache_key(self, study_id: str) -> str: | ||
return f"links:{study_id}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
|
||
import typing as t | ||
|
||
from antarest.core.interfaces.cache import ICache | ||
from antarest.study.business.model.link_model import LinkBaseDTO, LinkDTO | ||
from antarest.study.dao.link.cache_link_dao import CacheLinkDao | ||
from antarest.study.dao.link.storage_link_dao import StorageLinkDao | ||
from antarest.study.model import Study | ||
from antarest.study.storage.storage_service import StudyStorageService | ||
|
||
|
||
class CompositeLinkDAO: | ||
def __init__(self, storage_service: StudyStorageService, cache_service: ICache): | ||
self.storage_dao = StorageLinkDao(storage_service) | ||
self.cache_dao = CacheLinkDao(cache_service) | ||
|
||
def create_link(self, study: Study, link: LinkDTO) -> LinkDTO: | ||
self.cache_dao.invalidate(study.id) | ||
return self.storage_dao.create_link(study, link) | ||
|
||
def get_links(self, study: Study) -> t.List[LinkDTO]: | ||
links = self.cache_dao.get_links(study.id) | ||
if links is not None: | ||
return links | ||
|
||
links = self.storage_dao.get_links(study) | ||
|
||
for link in links: | ||
self.cache_dao.put(study.id, link) | ||
return links | ||
|
||
def update_link(self, study: Study, area_from: str, area_to: str, link_update_dto: LinkBaseDTO) -> LinkDTO: | ||
self.cache_dao.invalidate(study.id) | ||
return self.storage_dao.update_link(study, area_from, area_to, link_update_dto) | ||
|
||
def update_links( | ||
self, | ||
study: Study, | ||
update_links_by_ids: t.Mapping[t.Tuple[str, str], LinkBaseDTO], | ||
) -> t.Mapping[t.Tuple[str, str], LinkBaseDTO]: | ||
self.cache_dao.invalidate(study.id) | ||
return self.storage_dao.update_links(study, update_links_by_ids) | ||
|
||
def delete_link(self, study: Study, area_from: str, area_to: str) -> None: | ||
self.cache_dao.invalidate(study.id) | ||
self.storage_dao.delete_link(study, area_from, area_to) |
Oops, something went wrong.