Skip to content

Commit 58b66e6

Browse files
Clara Brasseurceb8
authored andcommitted
rethinking some of the caching
1 parent b87fc3b commit 58b66e6

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
@@ -114,15 +114,15 @@ def request_file(self, cache_location):
114114
fn = os.path.join(cache_location, self.hash() + ".pickle")
115115
return fn
116116

117-
def from_cache(self, cache_location):
117+
def from_cache(self, cache_location, cache_timeout):
118118
request_file = self.request_file(cache_location)
119119
try:
120-
if conf.default_cache_timeout is None:
120+
if cache_timeout is None:
121121
expired = False
122122
else:
123123
current_time = datetime.utcnow()
124124
cache_time = datetime.utcfromtimestamp(os.path.getmtime(request_file))
125-
expired = ((current_time-cache_time) > timedelta(seconds=conf.default_cache_timeout))
125+
expired = ((current_time-cache_time) > timedelta(seconds=cache_timeout))
126126
if not expired:
127127
with open(request_file, "rb") as f:
128128
response = pickle.load(f)
@@ -199,6 +199,8 @@ def __init__(self):
199199

200200
self.name = self.__class__.__name__.split("Class")[0]
201201
self._cache_active = conf.use_cache
202+
self.use_cache = conf.use_cache
203+
self.cache_timeout = conf.default_cache_timeout
202204

203205

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

242261
def _request(self, method, url,
243262
params=None, data=None, headers=None,
@@ -308,13 +327,10 @@ def _request(self, method, url,
308327
timeout=timeout,
309328
json=json
310329
)
311-
312-
# Set up cache
313-
if (cache is True) or ((cache is not False) and conf.use_cache):
314-
cache_location = os.path.join(conf.cache_location, self.name)
330+
331+
if (cache is not False) and self.use_cache:
315332
cache = True
316333
else:
317-
cache_location = None
318334
cache = False
319335

320336
if save:
@@ -343,10 +359,10 @@ def _request(self, method, url,
343359
allow_redirects=allow_redirects,
344360
json=json)
345361
else:
346-
response = query.from_cache(cache_location)
362+
response = query.from_cache(self.cache_location, self.cache_timeout)
347363
if not response:
348364
response = query.request(self._session,
349-
cache_location,
365+
self.cache_location,
350366
stream=stream,
351367
auth=auth,
352368
allow_redirects=allow_redirects,

0 commit comments

Comments
 (0)