Skip to content

Commit 02e5908

Browse files
Clara Brasseurceb8
authored andcommitted
rethinking some of the caching
1 parent 501af79 commit 02e5908

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

astroquery/query.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ def request_file(self, cache_location):
116116
fn = os.path.join(cache_location, self.hash() + ".pickle")
117117
return fn
118118

119-
def from_cache(self, cache_location):
119+
def from_cache(self, cache_location, cache_timeout):
120120
request_file = self.request_file(cache_location)
121121
try:
122-
if conf.default_cache_timeout is None:
122+
if cache_timeout is None:
123123
expired = False
124124
else:
125125
current_time = datetime.utcnow()
126126
cache_time = datetime.utcfromtimestamp(os.path.getmtime(request_file))
127-
expired = ((current_time-cache_time) > timedelta(seconds=conf.default_cache_timeout))
127+
expired = ((current_time-cache_time) > timedelta(seconds=cache_timeout))
128128
if not expired:
129129
with open(request_file, "rb") as f:
130130
response = pickle.load(f)
@@ -201,6 +201,8 @@ def __init__(self):
201201

202202
self.name = self.__class__.__name__.split("Class")[0]
203203
self._cache_active = conf.use_cache
204+
self.use_cache = conf.use_cache
205+
self.cache_timeout = conf.default_cache_timeout
204206

205207

206208
def __call__(self, *args, **kwargs):
@@ -240,6 +242,23 @@ def _response_hook(self, response, *args, **kwargs):
240242
f"{response.text}\n"
241243
f"-----------------------------------------", '\t')
242244
log.log(5, f"HTTP response\n{response_log}")
245+
246+
def clear_cache():
247+
"""Removes all cache files."""
248+
249+
cache_files = [x for x in os.listdir(self.cache_location) if x.endswidth("pickle")]
250+
for fle in cache_files:
251+
os.remove(fle)
252+
253+
def reset_cache_preferences():
254+
"""Resets cache preferences to default values"""
255+
256+
self.cache_location = os.path.join(
257+
conf.cache_location,
258+
self.__class__.__name__.split("Class")[0])
259+
260+
self.use_cache = conf.use_cache
261+
self.cache_timeout = conf.default_cache_timeout
243262

244263
def _request(self, method, url,
245264
params=None, data=None, headers=None,
@@ -310,13 +329,10 @@ def _request(self, method, url,
310329
timeout=timeout,
311330
json=json
312331
)
313-
314-
# Set up cache
315-
if (cache is True) or ((cache is not False) and conf.use_cache):
316-
cache_location = os.path.join(conf.cache_location, self.name)
332+
333+
if (cache is not False) and self.use_cache:
317334
cache = True
318335
else:
319-
cache_location = None
320336
cache = False
321337

322338
if save:
@@ -345,10 +361,10 @@ def _request(self, method, url,
345361
allow_redirects=allow_redirects,
346362
json=json)
347363
else:
348-
response = query.from_cache(cache_location)
364+
response = query.from_cache(self.cache_location, self.cache_timeout)
349365
if not response:
350366
response = query.request(self._session,
351-
cache_location,
367+
self.cache_location,
352368
stream=stream,
353369
auth=auth,
354370
allow_redirects=allow_redirects,

0 commit comments

Comments
 (0)