Skip to content

Commit f57d7f5

Browse files
authored
Fix cache key mismatch (#257)
* Fix cache key mismatch (#256) * Fix code quality for CI * Add #256, #257 to `doc/whatsnew.rst` * Expand unit test for caching behavior * Add dummy response for new unit test * Fix missing commas * Apply `ruff format`
1 parent abf1d43 commit f57d7f5

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

doc/whatsnew.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Next release
1414
with both :py:`version=...` and :py:`urn=...` would raise :class:`ValueError`
1515
even if the two were in agreement.
1616
- Fix two regressions in :func:`.to_pandas` introduced in v2.23.0 (:issue:`251`, :pull:`252`).
17+
- Fix false cache hits and misses (thanks :gh-user:`benfrankel` for :issue:`256`, :pull:`257`).
1718
- Fix a bug where supplying `references=...` to ESTAT or EMPL would raise :class:`ValueError` (thanks :gh-user:`benfrankel` for :issue:`259`, :pull:`260`).
1819

1920
v2.23.1 (2025-10-01)

sdmx/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def get(
339339
use_cache: bool = False,
340340
dry_run: bool = False,
341341
**kwargs,
342-
) -> "sdmx.message.Message":
342+
) -> "sdmx.message.Message | requests.Request":
343343
"""Retrieve SDMX data or metadata.
344344
345345
(Meta)data is retrieved from the :attr:`source` of the current Client. The
@@ -517,8 +517,8 @@ def get(
517517
msg = self.source.finish_message(msg, self, **kwargs)
518518

519519
# store in memory cache if needed
520-
if use_cache:
521-
self.cache[req.url] = msg
520+
if use_cache and req_prepared.url:
521+
self.cache[req_prepared.url] = msg
522522

523523
return msg
524524

sdmx/testing/data.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,17 @@ def add_responses(session: "Session", file_cache_path: Path, source: "Source") -
263263
"userdefinedoperatorscheme",
264264
"vtlmappingscheme",
265265
):
266-
url = f"{source.url}/{endpoint}/{source.id}/all/latest"
267-
save_response(session, method="GET", url=url, content=content, headers=headers)
266+
for url in (
267+
f"{source.url}/{endpoint}/{source.id}/all/latest",
268+
f"{source.url}/{endpoint}/{source.id}/all/latest?references=children",
269+
):
270+
save_response(
271+
session,
272+
method="GET",
273+
url=url,
274+
content=content,
275+
headers=headers,
276+
)
268277

269278
for url in (
270279
f"{source.url}/availableconstraint",

sdmx/tests/test_client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,26 @@ def test_init(self) -> None:
7070
sdmx.Client("noagency")
7171

7272
# Regular methods
73-
def test_clear_cache(self, client: "Client") -> None:
73+
def test_cache(self, client: "Client") -> None:
74+
# Cache starts empty
75+
assert not client.cache
76+
77+
# Response gets cached by the correct URL on cache miss
78+
req = client.get("dataflow", references="children", dry_run=True)
79+
assert isinstance(req, PreparedRequest)
80+
msg0 = client.get("dataflow", references="children", use_cache=True)
81+
assert len(client.cache) == 1
82+
assert req.url is not None and client.cache[req.url] is msg0
83+
84+
# Cached response gets returned on cache hit
85+
msg1 = client.get("dataflow", references="children", use_cache=True)
86+
assert msg1 is msg0
87+
assert len(client.cache) == 1
88+
assert req.url is not None and client.cache[req.url] is msg0
89+
90+
# Clearing the cache works
7491
client.clear_cache()
92+
assert not client.cache
7593

7694
def test_session_attrs0(
7795
self, caplog: "pytest.LogCaptureFixture", client: "Client"

0 commit comments

Comments
 (0)