@@ -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 """
0 commit comments