Skip to content

Commit c9617f9

Browse files
committed
implement reset_cache
1 parent 2b2db96 commit c9617f9

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

openml/_api/clients/http.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def request(
322322
path: str,
323323
*,
324324
use_cache: bool = False,
325+
reset_cache: bool = False,
325326
use_api_key: bool = False,
326327
**request_kwargs: Any,
327328
) -> Response:
@@ -345,7 +346,7 @@ def request(
345346
timeout = request_kwargs.pop("timeout", self.timeout)
346347
files = request_kwargs.pop("files", None)
347348

348-
if use_cache and self.cache is not None:
349+
if use_cache and not reset_cache and self.cache is not None:
349350
cache_key = self.cache.get_key(url, params)
350351
try:
351352
return self.cache.load(cache_key)
@@ -379,6 +380,7 @@ def request(
379380
assert response is not None
380381

381382
if use_cache and self.cache is not None:
383+
cache_key = self.cache.get_key(url, params)
382384
self.cache.save(cache_key, response)
383385

384386
return response
@@ -388,13 +390,15 @@ def get(
388390
path: str,
389391
*,
390392
use_cache: bool = False,
393+
reset_cache: bool = False,
391394
use_api_key: bool = False,
392395
**request_kwargs: Any,
393396
) -> Response:
394397
return self.request(
395398
method="GET",
396399
path=path,
397400
use_cache=use_cache,
401+
reset_cache=reset_cache,
398402
use_api_key=use_api_key,
399403
**request_kwargs,
400404
)

tests/test_api/test_http.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ def test_get_cache_expires(self):
103103
self.assertEqual(response2.status_code, 200)
104104
self.assertEqual(response1.content, response2.content)
105105

106+
@pytest.mark.uses_test_server()
107+
def test_get_reset_cache(self):
108+
path = "task/1"
109+
110+
url = self._get_url(path=path)
111+
key = self.cache.get_key(url, {})
112+
cache_path = self.cache._key_to_path(key) / "meta.json"
113+
114+
response1 = self.http_client.get(path, use_cache=True)
115+
response1_cache_time_stamp = cache_path.stat().st_ctime
116+
117+
response2 = self.http_client.get(path, use_cache=True, reset_cache=True)
118+
response2_cache_time_stamp = cache_path.stat().st_ctime
119+
120+
self.assertNotEqual(response1_cache_time_stamp, response2_cache_time_stamp)
121+
self.assertEqual(response2.status_code, 200)
122+
self.assertEqual(response1.content, response2.content)
123+
106124
@pytest.mark.uses_test_server()
107125
def test_post_and_delete(self):
108126
task_xml = """

0 commit comments

Comments
 (0)