Skip to content

Commit 04f1110

Browse files
committed
Improve caching of list JSON
1 parent 25b22e8 commit 04f1110

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

solc_select/repositories.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from abc import ABC, abstractmethod
9+
from functools import lru_cache
910
from typing import Dict, List, Optional, Tuple
1011

1112
import requests
@@ -29,7 +30,6 @@ class AbstractSolcRepository(ABC):
2930

3031
def __init__(self, session: requests.Session) -> None:
3132
self.session = session
32-
self._versions_cache: Optional[Dict[str, str]] = None
3333

3434
@property
3535
@abstractmethod
@@ -43,19 +43,24 @@ def list_url(self) -> str:
4343
"""Get the URL for the list.json file containing version information."""
4444
pass
4545

46-
def get_available_versions(self) -> Dict[str, str]:
47-
"""Get available versions as a dict of version -> artifact_filename."""
48-
# Return cached data if available
49-
if self._versions_cache is not None:
50-
return self._versions_cache
46+
@lru_cache(maxsize=5) # noqa: B019
47+
def _fetch_list_json(self) -> Dict:
48+
"""Fetch and cache the list.json data from the repository.
5149
52-
# Fetch from network and cache
50+
Returns:
51+
The parsed JSON data from the list.json endpoint
52+
"""
5353
response = self.session.get(self.list_url)
5454
response.raise_for_status()
55-
all_releases = response.json()["releases"]
56-
filtered_versions = self._filter_versions(all_releases)
57-
self._versions_cache = filtered_versions
58-
return filtered_versions
55+
return response.json()
56+
57+
@property
58+
@lru_cache(maxsize=5) # noqa: B019
59+
def available_versions(self) -> Dict[str, str]:
60+
"""Get available versions as a dict of version -> artifact_filename."""
61+
list_data = self._fetch_list_json()
62+
all_releases = list_data["releases"]
63+
return self._filter_versions(all_releases)
5964

6065
def _filter_versions(self, releases: Dict[str, str]) -> Dict[str, str]:
6166
"""Filter versions based on repository-specific criteria.
@@ -71,9 +76,8 @@ def get_download_url(self, version: SolcVersion, artifact_filename: str) -> str:
7176

7277
def get_checksums(self, version: SolcVersion) -> Tuple[str, Optional[str]]:
7378
"""Get SHA256 and optional Keccak256 checksums for a version."""
74-
response = self.session.get(self.list_url)
75-
response.raise_for_status()
76-
builds = response.json()["builds"]
79+
list_data = self._fetch_list_json()
80+
builds = list_data["builds"]
7781

7882
version_str = str(version)
7983
matches = [b for b in builds if b["version"] == version_str]
@@ -107,7 +111,6 @@ def __init__(self, platform: Platform, session: requests.Session) -> None:
107111
platform_key = platform.get_soliditylang_key()
108112
self._base_url = f"https://binaries.soliditylang.org/{platform_key}/"
109113
self._list_url = f"https://binaries.soliditylang.org/{platform_key}/list.json"
110-
self._latest_cache: Optional[SolcVersion] = None
111114

112115
@property
113116
def base_url(self) -> str:
@@ -121,19 +124,13 @@ def supports_version(self, version: SolcVersion, platform: Platform) -> bool:
121124
"""Check if this repository supports the version on the platform."""
122125
return version.is_compatible_with_platform(platform)
123126

124-
def get_latest_version(self) -> SolcVersion:
127+
@property
128+
@lru_cache(maxsize=5) # noqa: B019
129+
def latest_version(self) -> SolcVersion:
125130
"""Get the latest available version."""
126-
# Return cached data if available
127-
if self._latest_cache is not None:
128-
return self._latest_cache
129-
130-
# Fetch from network and cache
131-
response = self.session.get(self.list_url)
132-
response.raise_for_status()
133-
latest_str = response.json()["latestRelease"]
134-
latest_version = SolcVersion.parse(latest_str)
135-
self._latest_cache = latest_version
136-
return latest_version
131+
list_data = self._fetch_list_json()
132+
latest_str = list_data["latestRelease"]
133+
return SolcVersion.parse(latest_str)
137134

138135

139136
class CryticRepository(AbstractSolcRepository):
@@ -225,7 +222,7 @@ def get_available_versions(self) -> Dict[str, str]:
225222

226223
for repo in self.repositories:
227224
try:
228-
versions = repo.get_available_versions()
225+
versions = repo.available_versions
229226
all_versions.update(versions)
230227
except requests.RequestException:
231228
# Continue if one repository fails
@@ -247,7 +244,7 @@ def get_latest_version(self) -> SolcVersion:
247244
"""Get the latest version from the main repository."""
248245
main_repo = self.repositories[0]
249246
if isinstance(main_repo, SoliditylangRepository):
250-
return main_repo.get_latest_version()
247+
return main_repo.latest_version
251248

252249
# Fallback: parse from available versions
253250
versions = self.get_available_versions()

solc_select/services/artifact_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def create_artifact_metadata(self, version: SolcVersion) -> SolcArtifact:
111111
repo = self.repository.get_repository_for_version(version)
112112

113113
# Get available versions to find the artifact filename
114-
available = repo.get_available_versions()
114+
available = repo.available_versions
115115
version_str = str(version)
116116

117117
if version_str not in available:

0 commit comments

Comments
 (0)