@@ -119,9 +119,12 @@ def request_file(self, cache_location):
119
119
def from_cache (self , cache_location ):
120
120
request_file = self .request_file (cache_location )
121
121
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 ))
125
128
if not expired :
126
129
with open (request_file , "rb" ) as f :
127
130
response = pickle .load (f )
@@ -195,7 +198,10 @@ def __init__(self):
195
198
paths .get_cache_dir (), 'astroquery' ,
196
199
self .__class__ .__name__ .split ("Class" )[0 ])
197
200
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
+
199
205
200
206
def __call__ (self , * args , ** kwargs ):
201
207
""" init a fresh copy of self """
@@ -271,6 +277,7 @@ def _request(self, method, url,
271
277
somewhere other than `BaseQuery.cache_location`
272
278
timeout : int
273
279
cache : bool
280
+ Override global cache settings.
274
281
verify : bool
275
282
Verify the server's TLS certificate?
276
283
(see http://docs.python-requests.org/en/master/_modules/requests/sessions/?highlight=verify)
@@ -303,12 +310,22 @@ def _request(self, method, url,
303
310
timeout = timeout ,
304
311
json = json
305
312
)
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
+
306
322
if save :
307
323
local_filename = url .split ('/' )[- 1 ]
308
324
if os .name == 'nt' :
309
325
# Windows doesn't allow special characters in filenames like
310
326
# ":" so replace them with an underscore
311
327
local_filename = local_filename .replace (':' , '_' )
328
+
312
329
local_filepath = os .path .join (savedir or self .cache_location or '.' , local_filename )
313
330
314
331
response = self ._download_file (url , local_filepath , cache = cache ,
@@ -328,16 +345,17 @@ def _request(self, method, url,
328
345
allow_redirects = allow_redirects ,
329
346
json = json )
330
347
else :
331
- response = query .from_cache (self . cache_location )
348
+ response = query .from_cache (cache_location )
332
349
if not response :
333
350
response = query .request (self ._session ,
334
- self . cache_location ,
351
+ cache_location ,
335
352
stream = stream ,
336
353
auth = auth ,
337
354
allow_redirects = allow_redirects ,
338
355
verify = verify ,
339
356
json = json )
340
357
to_cache (response , query .request_file (self .cache_location ))
358
+
341
359
self ._last_query = query
342
360
return response
343
361
@@ -359,6 +377,7 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
359
377
supports HTTP "range" requests, the download will be continued
360
378
where it left off.
361
379
cache : bool
380
+ Cache downloaded file. Defaults to False.
362
381
method : "GET" or "POST"
363
382
head_safe : bool
364
383
"""
0 commit comments