Skip to content

Commit 501af79

Browse files
Clara Brasseurceb8
authored andcommitted
additional caching functionality
1 parent 3ebce51 commit 501af79

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

astroquery/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from ._astropy_init import *
1313
# ----------------------------------------------------------------------------
1414

15-
1615
import os
1716
import logging
1817

@@ -55,9 +54,16 @@ def _get_bibtex():
5554
class Conf(_config.ConfigNamespace):
5655

5756
default_cache_timeout = _config.ConfigItem(
58-
60.0*60.0*24.0,
57+
86400, # 24 hours
5958
'Astroquery-wide default cache timeout (seconds).'
6059
)
61-
60+
cache_location = _config.ConfigItem(
61+
os.path.join(_config.paths.get_cache_dir(), 'astroquery'),
62+
'Astroquery default cache location (within astropy cache).'
63+
)
64+
use_cache = _config.ConfigItem(
65+
True,
66+
"Astroquery global cache usage, False turns off all caching."
67+
)
6268

6369
conf = Conf()

astroquery/query.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ def request_file(self, cache_location):
119119
def from_cache(self, cache_location):
120120
request_file = self.request_file(cache_location)
121121
try:
122-
current_time = datetime.utcnow()
123-
cache_time = datetime.utcfromtimestamp(os.path.getmtime(request_file))
124-
expired = ((current_time-cache_time) > timedelta(seconds=conf.default_cache_timeout))
122+
if conf.default_cache_timeout is None:
123+
expired = False
124+
else:
125+
current_time = datetime.utcnow()
126+
cache_time = datetime.utcfromtimestamp(os.path.getmtime(request_file))
127+
expired = ((current_time-cache_time) > timedelta(seconds=conf.default_cache_timeout))
125128
if not expired:
126129
with open(request_file, "rb") as f:
127130
response = pickle.load(f)
@@ -195,7 +198,10 @@ def __init__(self):
195198
paths.get_cache_dir(), 'astroquery',
196199
self.__class__.__name__.split("Class")[0])
197200
os.makedirs(self.cache_location, exist_ok=True)
198-
self._cache_active = True
201+
202+
self.name = self.__class__.__name__.split("Class")[0]
203+
self._cache_active = conf.use_cache
204+
199205

200206
def __call__(self, *args, **kwargs):
201207
""" init a fresh copy of self """
@@ -271,6 +277,7 @@ def _request(self, method, url,
271277
somewhere other than `BaseQuery.cache_location`
272278
timeout : int
273279
cache : bool
280+
Override global cache settings.
274281
verify : bool
275282
Verify the server's TLS certificate?
276283
(see http://docs.python-requests.org/en/master/_modules/requests/sessions/?highlight=verify)
@@ -303,12 +310,22 @@ def _request(self, method, url,
303310
timeout=timeout,
304311
json=json
305312
)
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)
317+
cache = True
318+
else:
319+
cache_location = None
320+
cache = False
321+
306322
if save:
307323
local_filename = url.split('/')[-1]
308324
if os.name == 'nt':
309325
# Windows doesn't allow special characters in filenames like
310326
# ":" so replace them with an underscore
311327
local_filename = local_filename.replace(':', '_')
328+
312329
local_filepath = os.path.join(savedir or self.cache_location or '.', local_filename)
313330

314331
response = self._download_file(url, local_filepath, cache=cache,
@@ -328,16 +345,17 @@ def _request(self, method, url,
328345
allow_redirects=allow_redirects,
329346
json=json)
330347
else:
331-
response = query.from_cache(self.cache_location)
348+
response = query.from_cache(cache_location)
332349
if not response:
333350
response = query.request(self._session,
334-
self.cache_location,
351+
cache_location,
335352
stream=stream,
336353
auth=auth,
337354
allow_redirects=allow_redirects,
338355
verify=verify,
339356
json=json)
340357
to_cache(response, query.request_file(self.cache_location))
358+
341359
self._last_query = query
342360
return response
343361

@@ -359,6 +377,7 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
359377
supports HTTP "range" requests, the download will be continued
360378
where it left off.
361379
cache : bool
380+
Cache downloaded file. Defaults to False.
362381
method : "GET" or "POST"
363382
head_safe : bool
364383
"""

astroquery/setup_package.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
def get_package_data():
55
return {'astroquery': ['CITATION']}
6+

0 commit comments

Comments
 (0)