Skip to content

Commit

Permalink
Cache expensive data collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjkw31 committed Sep 10, 2024
1 parent a93c265 commit 15e370e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 12 additions & 1 deletion softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ class Environment:
state: Optional[State]
tags: list[str]
hidden: bool
cachedEnvs: list["Environment"] = None
envsUpdates: bool = True

requested: Optional[datetime.datetime] = None
build_start: Optional[datetime.datetime] = None
Expand All @@ -335,6 +337,9 @@ def iter(cls) -> list["Environment"]:
Returns:
Iterable[Environment]: An iterator of Environment objects.
"""
if not cls.envsUpdates:
return cls.cachedEnvs

statuses = BuildStatus.get_all()
if isinstance(statuses, BuilderError):
statuses = []
Expand Down Expand Up @@ -366,6 +371,9 @@ def iter(cls) -> list["Environment"]:
env.build_start = status.build_start
env.build_done = status.build_done

cls.cachedEnvs = environment_objects
cls.envsUpdates = False

return environment_objects

@classmethod
Expand Down Expand Up @@ -541,6 +549,7 @@ def create_new_env(
],
True,
)
cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "create environment folder")
except RuntimeError as e:
return InvalidInputError(
Expand Down Expand Up @@ -640,7 +649,7 @@ def store_metadata(cls, environment_path: Path, metadata: Box) -> None:
metadata.to_yaml(),
overwrite=True,
)

cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "update metadata")

@classmethod
Expand Down Expand Up @@ -677,6 +686,7 @@ def delete(cls, name: str, path: str) -> DeleteResponse: # type: ignore
"""
if artifacts.get(Path(path), name):
tree_oid = artifacts.delete_environment(name, path)
cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "delete environment")
return DeleteEnvironmentSuccess(
message="Successfully deleted the environment"
Expand Down Expand Up @@ -846,6 +856,7 @@ async def write_artifacts(
tree_oid = artifacts.create_files(
Path(folder_path), new_files, overwrite=True
)
cls.envsUpdates = True
artifacts.commit_and_push(tree_oid, "write artifact")
return WriteArtifactSuccess(
message="Successfully written artifact(s)",
Expand Down
8 changes: 6 additions & 2 deletions softpack_core/schemas/package_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class PackageCollection:
packages: list[PackageMultiVersion]

@classmethod
def iter(cls) -> Iterable["PackageMultiVersion"]:
def iter(cls) -> list["PackageMultiVersion"]:
"""Get an iterator over PackageCollection objects.
Returns:
Iterable[PackageCollection]: An iterator of PackageCollection
objects.
"""
return map(cls.from_package, app.spack.packages())
if app.spack.packagesUpdated:
cls.packages = list(map(cls.from_package, app.spack.packages()))
app.spack.packagesUpdated = False

return cls.packages

@classmethod
def from_package(cls, package: Package) -> PackageMultiVersion:
Expand Down
4 changes: 4 additions & 0 deletions softpack_core/spack.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Package(PackageBase):
class Spack:
"""Spack interface class."""

packages: list[Package]
packagesUpdated: bool = True

def __init__(
self,
spack_exe: str = "spack",
Expand Down Expand Up @@ -97,6 +100,7 @@ def store_packages_from_spack(
json.loads(jsonData),
)
)
self.packagesUpdated = True

def __readPackagesFromCacheOnce(self) -> Tuple[bytes, bool]:
if len(self.stored_packages) > 0 or self.cacheDir == "":
Expand Down

0 comments on commit 15e370e

Please sign in to comment.