Skip to content

Commit e181b3f

Browse files
author
Clara Brasseur
committed
rethinking some of the caching
1 parent 44bf250 commit e181b3f

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

astroquery/query.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ def request_file(self, cache_location):
104104
fn = os.path.join(cache_location, self.hash() + ".pickle")
105105
return fn
106106

107-
def from_cache(self, cache_location):
107+
def from_cache(self, cache_location, cache_timeout):
108108
request_file = self.request_file(cache_location)
109109
try:
110-
if conf.default_cache_timeout is None:
110+
if cache_timeout is None:
111111
expired = False
112112
else:
113113
current_time = datetime.utcnow()
114114
cache_time = datetime.utcfromtimestamp(os.path.getmtime(request_file))
115-
expired = ((current_time-cache_time) > timedelta(seconds=conf.default_cache_timeout))
115+
expired = ((current_time-cache_time) > timedelta(seconds=cache_timeout))
116116
if not expired:
117117
with open(request_file, "rb") as f:
118118
response = pickle.load(f)
@@ -169,13 +169,35 @@ def __init__(self):
169169
'astroquery/{vers} {olduseragent}'
170170
.format(vers=version.version,
171171
olduseragent=S.headers['User-Agent']))
172-
self.name = self.__class__.__name__.split("Class")[0]
173-
self._cache_active = conf.use_cache
172+
self.cache_location = os.path.join(
173+
conf.cache_location,
174+
self.__class__.__name__.split("Class")[0])
175+
176+
self.use_cache = conf.use_cache
177+
self.cache_timeout = conf.default_cache_timeout
174178

175179
def __call__(self, *args, **kwargs):
176180
""" init a fresh copy of self """
177181
return self.__class__(*args, **kwargs)
178182

183+
def clear_cache():
184+
"""Removes all cache files."""
185+
186+
cache_files = [x for x in os.listdir(self.cache_location) if x.endswidth("pickle")]
187+
for fle in cache_files:
188+
os.remove(fle)
189+
190+
def reset_cache_preferences():
191+
"""Resets cache preferences to default values"""
192+
193+
self.cache_location = os.path.join(
194+
conf.cache_location,
195+
self.__class__.__name__.split("Class")[0])
196+
197+
self.use_cache = conf.use_cache
198+
self.cache_timeout = conf.default_cache_timeout
199+
200+
179201
def _request(self, method, url, params=None, data=None, headers=None,
180202
files=None, save=False, savedir='', timeout=None, cache=None,
181203
stream=False, auth=None, continuation=True, verify=True):
@@ -233,13 +255,10 @@ def _request(self, method, url, params=None, data=None, headers=None,
233255
files=files,
234256
timeout=timeout
235257
)
236-
237-
# Set up cache
238-
if (cache is True) or ((cache is not False) and conf.use_cache):
239-
cache_location = os.path.join(conf.cache_location, self.name)
258+
259+
if (cache is not False) and self.use_cache:
240260
cache = True
241261
else:
242-
cache_location = None
243262
cache = False
244263

245264
if save:
@@ -248,25 +267,25 @@ def _request(self, method, url, params=None, data=None, headers=None,
248267
# Windows doesn't allow special characters in filenames like
249268
# ":" so replace them with an underscore
250269
local_filename = local_filename.replace(':', '_')
251-
local_filepath = os.path.join(savedir or cache_location or '.', local_filename)
270+
local_filepath = os.path.join(savedir or self.cache_location or '.', local_filename)
252271
self._download_file(url, local_filepath, cache=cache,
253272
continuation=continuation, method=method,
254273
auth=auth, **req_kwargs)
255274
return local_filepath
256275
else:
257276
query = AstroQuery(method, url, **req_kwargs)
258-
if ((cache_location is None) or (not cache)):
277+
if ((self.cache_location is None) or (cache is False)):
259278
response = query.request(self._session, stream=stream,
260279
auth=auth, verify=verify)
261280
else:
262-
response = query.from_cache(cache_location)
281+
response = query.from_cache(self.cache_location, self.cache_timeout)
263282
if not response:
264283
response = query.request(self._session,
265-
cache_location,
284+
self.cache_location,
266285
stream=stream,
267286
auth=auth,
268287
verify=verify)
269-
to_cache(response, query.request_file(cache_location))
288+
to_cache(response, query.request_file(self.cache_location))
270289
self._last_query = query
271290
return response
272291

0 commit comments

Comments
 (0)